home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / expmed.c < prev    next >
C/C++ Source or Header  |  1994-07-13  |  125KB  |  3,958 lines

  1. /* Medium-level subroutines: convert bit-field store and extract
  2.    and shifts, multiplies and divides to rtl instructions.
  3.    Copyright (C) 1987, 88, 89, 92, 93, 1994 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "tree.h"
  25. #include "flags.h"
  26. #include "insn-flags.h"
  27. #include "insn-codes.h"
  28. #include "insn-config.h"
  29. #include "expr.h"
  30. #include "real.h"
  31. #include "recog.h"
  32.  
  33. static void store_fixed_bit_field    PROTO((rtx, int, int, int, rtx, int));
  34. static void store_split_bit_field    PROTO((rtx, int, int, rtx, int));
  35. static rtx extract_fixed_bit_field    PROTO((enum machine_mode, rtx, int,
  36.                            int, int, rtx, int, int));
  37. static rtx mask_rtx            PROTO((enum machine_mode, int,
  38.                            int, int));
  39. static rtx lshift_value            PROTO((enum machine_mode, rtx,
  40.                            int, int));
  41. static rtx extract_split_bit_field    PROTO((rtx, int, int, int, int));
  42.  
  43. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  44.  
  45. /* Non-zero means divides or modulus operations are relatively cheap for
  46.    powers of two, so don't use branches; emit the operation instead. 
  47.    Usually, this will mean that the MD file will emit non-branch
  48.    sequences.  */
  49.  
  50. static int sdiv_pow2_cheap, smod_pow2_cheap;
  51.  
  52. #ifndef SLOW_UNALIGNED_ACCESS
  53. #define SLOW_UNALIGNED_ACCESS STRICT_ALIGNMENT
  54. #endif
  55.  
  56. /* For compilers that support multiple targets with different word sizes,
  57.    MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD.  An example
  58.    is the H8/300(H) compiler.  */
  59.  
  60. #ifndef MAX_BITS_PER_WORD
  61. #define MAX_BITS_PER_WORD BITS_PER_WORD
  62. #endif
  63.  
  64. /* Cost of various pieces of RTL.  */
  65. static int add_cost, negate_cost, zero_cost;
  66. static int shift_cost[MAX_BITS_PER_WORD];
  67. static int shiftadd_cost[MAX_BITS_PER_WORD];
  68. static int shiftsub_cost[MAX_BITS_PER_WORD];
  69.  
  70. void
  71. init_expmed ()
  72. {
  73.   char *free_point;
  74.   /* This is "some random pseudo register" for purposes of calling recog
  75.      to see what insns exist.  */
  76.   rtx reg = gen_rtx (REG, word_mode, 10000);
  77.   rtx shift_insn, shiftadd_insn, shiftsub_insn;
  78.   int dummy;
  79.   int m;
  80.  
  81.   start_sequence ();
  82.  
  83.   /* Since we are on the permanent obstack, we must be sure we save this
  84.      spot AFTER we call start_sequence, since it will reuse the rtl it
  85.      makes.  */
  86.  
  87.   free_point = (char *) oballoc (0);
  88.  
  89.   zero_cost = rtx_cost (const0_rtx, 0);
  90.   add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
  91.  
  92.   shift_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
  93.                    gen_rtx (ASHIFT, word_mode, reg,
  94.                         const0_rtx)));
  95.  
  96.   shiftadd_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
  97.                       gen_rtx (PLUS, word_mode,
  98.                            gen_rtx (MULT, word_mode,
  99.                             reg, const0_rtx),
  100.                            reg)));
  101.  
  102.   shiftsub_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
  103.                       gen_rtx (MINUS, word_mode,
  104.                            gen_rtx (MULT, word_mode,
  105.                              reg, const0_rtx),
  106.                         reg)));
  107.  
  108.   init_recog ();
  109.  
  110.   shift_cost[0] = 0;
  111.   shiftadd_cost[0] = shiftsub_cost[0] = add_cost;
  112.  
  113.   for (m = 1; m < BITS_PER_WORD; m++)
  114.     {
  115.       shift_cost[m] = shiftadd_cost[m] = shiftsub_cost[m] = 32000;
  116.  
  117.       XEXP (SET_SRC (PATTERN (shift_insn)), 1) = GEN_INT (m);
  118.       if (recog (PATTERN (shift_insn), shift_insn, &dummy) >= 0)
  119.     shift_cost[m] = rtx_cost (SET_SRC (PATTERN (shift_insn)), SET);
  120.  
  121.       XEXP (XEXP (SET_SRC (PATTERN (shiftadd_insn)), 0), 1)
  122.     = GEN_INT ((HOST_WIDE_INT) 1 << m);
  123.       if (recog (PATTERN (shiftadd_insn), shiftadd_insn, &dummy) >= 0)
  124.     shiftadd_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftadd_insn)), SET);
  125.  
  126.       XEXP (XEXP (SET_SRC (PATTERN (shiftsub_insn)), 0), 1)
  127.     = GEN_INT ((HOST_WIDE_INT) 1 << m);
  128.       if (recog (PATTERN (shiftsub_insn), shiftsub_insn, &dummy) >= 0)
  129.     shiftsub_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftsub_insn)), SET);
  130.     }
  131.  
  132.   negate_cost = rtx_cost (gen_rtx (NEG, word_mode, reg), SET);
  133.  
  134.   sdiv_pow2_cheap
  135.     = (rtx_cost (gen_rtx (DIV, word_mode, reg, GEN_INT (32)), SET)
  136.        <= 2 * add_cost);
  137.   smod_pow2_cheap
  138.     = (rtx_cost (gen_rtx (MOD, word_mode, reg, GEN_INT (32)), SET)
  139.        <= 2 * add_cost);
  140.  
  141.   /* Free the objects we just allocated.  */
  142.   end_sequence ();
  143.   obfree (free_point);
  144. }
  145.  
  146. /* Return an rtx representing minus the value of X.
  147.    MODE is the intended mode of the result,
  148.    useful if X is a CONST_INT.  */
  149.  
  150. rtx
  151. negate_rtx (mode, x)
  152.      enum machine_mode mode;
  153.      rtx x;
  154. {
  155.   if (GET_CODE (x) == CONST_INT)
  156.     {
  157.       HOST_WIDE_INT val = - INTVAL (x);
  158.       if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
  159.     {
  160.       /* Sign extend the value from the bits that are significant.  */
  161.       if (val & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
  162.         val |= (HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (mode);
  163.       else
  164.         val &= ((HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (mode)) - 1;
  165.     }
  166.       return GEN_INT (val);
  167.     }
  168.   else
  169.     return expand_unop (GET_MODE (x), neg_optab, x, NULL_RTX, 0);
  170. }
  171.  
  172. /* Generate code to store value from rtx VALUE
  173.    into a bit-field within structure STR_RTX
  174.    containing BITSIZE bits starting at bit BITNUM.
  175.    FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
  176.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
  177.    TOTAL_SIZE is the size of the structure in bytes, or -1 if varying.  */
  178.  
  179. /* ??? Note that there are two different ideas here for how
  180.    to determine the size to count bits within, for a register.
  181.    One is BITS_PER_WORD, and the other is the size of operand 3
  182.    of the insv pattern.  (The latter assumes that an n-bit machine
  183.    will be able to insert bit fields up to n bits wide.)
  184.    It isn't certain that either of these is right.
  185.    extract_bit_field has the same quandary.  */
  186.  
  187. rtx
  188. store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align, total_size)
  189.      rtx str_rtx;
  190.      register int bitsize;
  191.      int bitnum;
  192.      enum machine_mode fieldmode;
  193.      rtx value;
  194.      int align;
  195.      int total_size;
  196. {
  197.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  198.   register int offset = bitnum / unit;
  199.   register int bitpos = bitnum % unit;
  200.   register rtx op0 = str_rtx;
  201.  
  202.   if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
  203.     abort ();
  204.  
  205.   /* Discount the part of the structure before the desired byte.
  206.      We need to know how many bytes are safe to reference after it.  */
  207.   if (total_size >= 0)
  208.     total_size -= (bitpos / BIGGEST_ALIGNMENT
  209.            * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  210.  
  211.   while (GET_CODE (op0) == SUBREG)
  212.     {
  213.       /* The following line once was done only if WORDS_BIG_ENDIAN,
  214.      but I think that is a mistake.  WORDS_BIG_ENDIAN is
  215.      meaningful at a much higher level; when structures are copied
  216.      between memory and regs, the higher-numbered regs
  217.      always get higher addresses.  */
  218.       offset += SUBREG_WORD (op0);
  219.       /* We used to adjust BITPOS here, but now we do the whole adjustment
  220.      right after the loop.  */
  221.       op0 = SUBREG_REG (op0);
  222.     }
  223.  
  224. #if BYTES_BIG_ENDIAN
  225.   /* If OP0 is a register, BITPOS must count within a word.
  226.      But as we have it, it counts within whatever size OP0 now has.
  227.      On a bigendian machine, these are not the same, so convert.  */
  228.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  229.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  230. #endif
  231.  
  232.   value = protect_from_queue (value, 0);
  233.  
  234.   if (flag_force_mem)
  235.     value = force_not_mem (value);
  236.  
  237.   /* Note that the adjustment of BITPOS above has no effect on whether
  238.      BITPOS is 0 in a REG bigger than a word.  */
  239.   if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
  240.       && (GET_CODE (op0) != MEM
  241.       || ! SLOW_UNALIGNED_ACCESS
  242.       || (offset * BITS_PER_UNIT % bitsize == 0
  243.           && align % GET_MODE_SIZE (fieldmode) == 0))
  244.       && bitpos == 0 && bitsize == GET_MODE_BITSIZE (fieldmode))
  245.     {
  246.       /* Storing in a full-word or multi-word field in a register
  247.      can be done with just SUBREG.  */
  248.       if (GET_MODE (op0) != fieldmode)
  249.     {
  250.       if (GET_CODE (op0) == REG)
  251.         op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
  252.       else
  253.         op0 = change_address (op0, fieldmode,
  254.                   plus_constant (XEXP (op0, 0), offset));
  255.     }
  256.       emit_move_insn (op0, value);
  257.       return value;
  258.     }
  259.  
  260.   /* Storing an lsb-aligned field in a register
  261.      can be done with a movestrict instruction.  */
  262.  
  263.   if (GET_CODE (op0) != MEM
  264. #if BYTES_BIG_ENDIAN
  265.       && bitpos + bitsize == unit
  266. #else
  267.       && bitpos == 0
  268. #endif
  269.       && bitsize == GET_MODE_BITSIZE (fieldmode)
  270.       && (GET_MODE (op0) == fieldmode
  271.       || (movstrict_optab->handlers[(int) fieldmode].insn_code
  272.           != CODE_FOR_nothing)))
  273.     {
  274.       /* Get appropriate low part of the value being stored.  */
  275.       if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
  276.     value = gen_lowpart (fieldmode, value);
  277.       else if (!(GET_CODE (value) == SYMBOL_REF
  278.          || GET_CODE (value) == LABEL_REF
  279.          || GET_CODE (value) == CONST))
  280.     value = convert_to_mode (fieldmode, value, 0);
  281.  
  282.       if (GET_MODE (op0) == fieldmode)
  283.     emit_move_insn (op0, value);
  284.       else
  285.     {
  286.       int icode = movstrict_optab->handlers[(int) fieldmode].insn_code;
  287.       if(! (*insn_operand_predicate[icode][1]) (value, fieldmode))
  288.         value = copy_to_mode_reg (fieldmode, value);
  289.       emit_insn (GEN_FCN (icode)
  290.            (gen_rtx (SUBREG, fieldmode, op0, offset), value));
  291.     }
  292.       return value;
  293.     }
  294.  
  295.   /* Handle fields bigger than a word.  */
  296.  
  297.   if (bitsize > BITS_PER_WORD)
  298.     {
  299.       /* Here we transfer the words of the field
  300.      in the order least significant first.
  301.      This is because the most significant word is the one which may
  302.      be less than full.  */
  303.  
  304.       int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
  305.       int i;
  306.  
  307.       /* This is the mode we must force value to, so that there will be enough
  308.      subwords to extract.  Note that fieldmode will often (always?) be
  309.      VOIDmode, because that is what store_field uses to indicate that this
  310.      is a bit field, but passing VOIDmode to operand_subword_force will
  311.      result in an abort.  */
  312.       fieldmode = mode_for_size (nwords * BITS_PER_WORD, MODE_INT, 0);
  313.  
  314.       for (i = 0; i < nwords; i++)
  315.     {
  316.       /* If I is 0, use the low-order word in both field and target;
  317.          if I is 1, use the next to lowest word; and so on.  */
  318.       int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
  319.       int bit_offset = (WORDS_BIG_ENDIAN
  320.                 ? MAX (bitsize - (i + 1) * BITS_PER_WORD, 0)
  321.                 : i * BITS_PER_WORD);
  322.       store_bit_field (op0, MIN (BITS_PER_WORD,
  323.                      bitsize - i * BITS_PER_WORD),
  324.                bitnum + bit_offset, word_mode,
  325.                operand_subword_force (value, wordnum,
  326.                           (GET_MODE (value) == VOIDmode
  327.                            ? fieldmode
  328.                            : GET_MODE (value))),
  329.                align, total_size);
  330.     }
  331.       return value;
  332.     }
  333.  
  334.   /* From here on we can assume that the field to be stored in is
  335.      a full-word (whatever type that is), since it is shorter than a word.  */
  336.  
  337.   /* OFFSET is the number of words or bytes (UNIT says which)
  338.      from STR_RTX to the first word or byte containing part of the field.  */
  339.  
  340.   if (GET_CODE (op0) == REG)
  341.     {
  342.       if (offset != 0
  343.       || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
  344.     op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
  345.                op0, offset);
  346.       offset = 0;
  347.     }
  348.   else
  349.     {
  350.       op0 = protect_from_queue (op0, 1);
  351.     }
  352.  
  353.   /* If VALUE is a floating-point mode, access it as an integer of the
  354.      corresponding size.  This can occur on a machine with 64 bit registers
  355.      that uses SFmode for float.  This can also occur for unaligned float
  356.      structure fields.  */
  357.   if (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT)
  358.     {
  359.       if (GET_CODE (value) != REG)
  360.     value = copy_to_reg (value);
  361.       value = gen_rtx (SUBREG, word_mode, value, 0);
  362.     }
  363.  
  364.   /* Now OFFSET is nonzero only if OP0 is memory
  365.      and is therefore always measured in bytes.  */
  366.  
  367. #ifdef HAVE_insv
  368.   if (HAVE_insv
  369.       && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
  370.       /* Ensure insv's size is wide enough for this field.  */
  371.       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_insv][3])
  372.       >= bitsize))
  373.     {
  374.       int xbitpos = bitpos;
  375.       rtx value1;
  376.       rtx xop0 = op0;
  377.       rtx last = get_last_insn ();
  378.       rtx pat;
  379.       enum machine_mode maxmode
  380.     = insn_operand_mode[(int) CODE_FOR_insv][3];
  381.  
  382.       int save_volatile_ok = volatile_ok;
  383.       volatile_ok = 1;
  384.  
  385.       /* If this machine's insv can only insert into a register, or if we
  386.      are to force MEMs into a register, copy OP0 into a register and
  387.      save it back later.  */
  388.       if (GET_CODE (op0) == MEM
  389.       && (flag_force_mem
  390.           || ! ((*insn_operand_predicate[(int) CODE_FOR_insv][0])
  391.             (op0, VOIDmode))))
  392.     {
  393.       rtx tempreg;
  394.       enum machine_mode bestmode;
  395.  
  396.       /* Get the mode to use for inserting into this field.  If OP0 is
  397.          BLKmode, get the smallest mode consistent with the alignment. If
  398.          OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
  399.          mode. Otherwise, use the smallest mode containing the field.  */
  400.  
  401.       if (GET_MODE (op0) == BLKmode
  402.           || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
  403.         bestmode
  404.           = get_best_mode (bitsize, bitnum, align * BITS_PER_UNIT, maxmode,
  405.                    MEM_VOLATILE_P (op0));
  406.       else
  407.         bestmode = GET_MODE (op0);
  408.  
  409.       if (bestmode == VOIDmode
  410.           || (STRICT_ALIGNMENT && GET_MODE_SIZE (bestmode) > align))
  411.         goto insv_loses;
  412.  
  413.       /* Adjust address to point to the containing unit of that mode.  */
  414.       unit = GET_MODE_BITSIZE (bestmode);
  415.       /* Compute offset as multiple of this unit, counting in bytes.  */
  416.       offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  417.       bitpos = bitnum % unit;
  418.       op0 = change_address (op0, bestmode, 
  419.                 plus_constant (XEXP (op0, 0), offset));
  420.  
  421.       /* Fetch that unit, store the bitfield in it, then store the unit.  */
  422.       tempreg = copy_to_reg (op0);
  423.       store_bit_field (tempreg, bitsize, bitpos, fieldmode, value,
  424.                align, total_size);
  425.       emit_move_insn (op0, tempreg);
  426.       return value;
  427.     }
  428.       volatile_ok = save_volatile_ok;
  429.  
  430.       /* Add OFFSET into OP0's address.  */
  431.       if (GET_CODE (xop0) == MEM)
  432.     xop0 = change_address (xop0, byte_mode,
  433.                    plus_constant (XEXP (xop0, 0), offset));
  434.  
  435.       /* If xop0 is a register, we need it in MAXMODE
  436.      to make it acceptable to the format of insv.  */
  437.       if (GET_CODE (xop0) == SUBREG)
  438.     /* We can't just change the mode, because this might clobber op0,
  439.        and we will need the original value of op0 if insv fails.  */
  440.     xop0 = gen_rtx (SUBREG, maxmode, SUBREG_REG (xop0), SUBREG_WORD (xop0));
  441.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
  442.     xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
  443.  
  444.       /* On big-endian machines, we count bits from the most significant.
  445.      If the bit field insn does not, we must invert.  */
  446.  
  447. #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
  448.       xbitpos = unit - bitsize - xbitpos;
  449. #endif
  450.       /* We have been counting XBITPOS within UNIT.
  451.      Count instead within the size of the register.  */
  452. #if BITS_BIG_ENDIAN
  453.       if (GET_CODE (xop0) != MEM)
  454.     xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
  455. #endif
  456.       unit = GET_MODE_BITSIZE (maxmode);
  457.  
  458.       /* Convert VALUE to maxmode (which insv insn wants) in VALUE1.  */
  459.       value1 = value;
  460.       if (GET_MODE (value) != maxmode)
  461.     {
  462.       if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
  463.         {
  464.           /* Optimization: Don't bother really extending VALUE
  465.          if it has all the bits we will actually use.  However,
  466.          if we must narrow it, be sure we do it correctly.  */
  467.  
  468.           if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
  469.         {
  470.           /* Avoid making subreg of a subreg, or of a mem.  */
  471.           if (GET_CODE (value1) != REG)
  472.         value1 = copy_to_reg (value1);
  473.           value1 = gen_rtx (SUBREG, maxmode, value1, 0);
  474.         }
  475.           else
  476.         value1 = gen_lowpart (maxmode, value1);
  477.         }
  478.       else if (!CONSTANT_P (value))
  479.         /* Parse phase is supposed to make VALUE's data type
  480.            match that of the component reference, which is a type
  481.            at least as wide as the field; so VALUE should have
  482.            a mode that corresponds to that type.  */
  483.         abort ();
  484.     }
  485.  
  486.       /* If this machine's insv insists on a register,
  487.      get VALUE1 into a register.  */
  488.       if (! ((*insn_operand_predicate[(int) CODE_FOR_insv][3])
  489.          (value1, maxmode)))
  490.     value1 = force_reg (maxmode, value1);
  491.  
  492.       pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
  493.       if (pat)
  494.     emit_insn (pat);
  495.       else
  496.         {
  497.       delete_insns_since (last);
  498.       store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  499.     }
  500.     }
  501.   else
  502.     insv_loses:
  503. #endif
  504.     /* Insv is not available; store using shifts and boolean ops.  */
  505.     store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  506.   return value;
  507. }
  508.  
  509. /* Use shifts and boolean operations to store VALUE
  510.    into a bit field of width BITSIZE
  511.    in a memory location specified by OP0 except offset by OFFSET bytes.
  512.      (OFFSET must be 0 if OP0 is a register.)
  513.    The field starts at position BITPOS within the byte.
  514.     (If OP0 is a register, it may be a full word or a narrower mode,
  515.      but BITPOS still counts within a full word,
  516.      which is significant on bigendian machines.)
  517.    STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
  518.  
  519.    Note that protect_from_queue has already been done on OP0 and VALUE.  */
  520.  
  521. static void
  522. store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
  523.      register rtx op0;
  524.      register int offset, bitsize, bitpos;
  525.      register rtx value;
  526.      int struct_align;
  527. {
  528.   register enum machine_mode mode;
  529.   int total_bits = BITS_PER_WORD;
  530.   rtx subtarget, temp;
  531.   int all_zero = 0;
  532.   int all_one = 0;
  533.  
  534.   /* There is a case not handled here:
  535.      a structure with a known alignment of just a halfword
  536.      and a field split across two aligned halfwords within the structure.
  537.      Or likewise a structure with a known alignment of just a byte
  538.      and a field split across two bytes.
  539.      Such cases are not supposed to be able to occur.  */
  540.  
  541.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  542.     {
  543.       if (offset != 0)
  544.     abort ();
  545.       /* Special treatment for a bit field split across two registers.  */
  546.       if (bitsize + bitpos > BITS_PER_WORD)
  547.     {
  548.       store_split_bit_field (op0, bitsize, bitpos,
  549.                  value, BITS_PER_WORD);
  550.       return;
  551.     }
  552.     }
  553.   else
  554.     {
  555.       /* Get the proper mode to use for this field.  We want a mode that
  556.      includes the entire field.  If such a mode would be larger than
  557.      a word, we won't be doing the extraction the normal way.  */
  558.  
  559.       mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
  560.                 struct_align * BITS_PER_UNIT, word_mode,
  561.                 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
  562.  
  563.       if (mode == VOIDmode)
  564.     {
  565.       /* The only way this should occur is if the field spans word
  566.          boundaries.  */
  567.       store_split_bit_field (op0,
  568.                  bitsize, bitpos + offset * BITS_PER_UNIT,
  569.                  value, struct_align);
  570.       return;
  571.     }
  572.  
  573.       total_bits = GET_MODE_BITSIZE (mode);
  574.  
  575.       /* Make sure bitpos is valid for the chosen mode.  Adjust BITPOS to
  576.      be be in the range 0 to total_bits-1, and put any excess bytes in
  577.      OFFSET.  */
  578.       if (bitpos >= total_bits)
  579.     {
  580.       offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
  581.       bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
  582.              * BITS_PER_UNIT);
  583.     }
  584.  
  585.       /* Get ref to an aligned byte, halfword, or word containing the field.
  586.      Adjust BITPOS to be position within a word,
  587.      and OFFSET to be the offset of that word.
  588.      Then alter OP0 to refer to that word.  */
  589.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  590.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  591.       op0 = change_address (op0, mode,
  592.                 plus_constant (XEXP (op0, 0), offset));
  593.     }
  594.  
  595.   mode = GET_MODE (op0);
  596.  
  597.   /* Now MODE is either some integral mode for a MEM as OP0,
  598.      or is a full-word for a REG as OP0.  TOTAL_BITS corresponds.
  599.      The bit field is contained entirely within OP0.
  600.      BITPOS is the starting bit number within OP0.
  601.      (OP0's mode may actually be narrower than MODE.)  */
  602.  
  603. #if BYTES_BIG_ENDIAN
  604.   /* BITPOS is the distance between our msb
  605.      and that of the containing datum.
  606.      Convert it to the distance from the lsb.  */
  607.  
  608.   bitpos = total_bits - bitsize - bitpos;
  609. #endif
  610.   /* Now BITPOS is always the distance between our lsb
  611.      and that of OP0.  */
  612.  
  613.   /* Shift VALUE left by BITPOS bits.  If VALUE is not constant,
  614.      we must first convert its mode to MODE.  */
  615.  
  616.   if (GET_CODE (value) == CONST_INT)
  617.     {
  618.       register HOST_WIDE_INT v = INTVAL (value);
  619.  
  620.       if (bitsize < HOST_BITS_PER_WIDE_INT)
  621.     v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
  622.  
  623.       if (v == 0)
  624.     all_zero = 1;
  625.       else if ((bitsize < HOST_BITS_PER_WIDE_INT
  626.         && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
  627.            || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
  628.     all_one = 1;
  629.  
  630.       value = lshift_value (mode, value, bitpos, bitsize);
  631.     }
  632.   else
  633.     {
  634.       int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
  635.               && bitpos + bitsize != GET_MODE_BITSIZE (mode));
  636.  
  637.       if (GET_MODE (value) != mode)
  638.     {
  639.       if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
  640.           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
  641.         value = gen_lowpart (mode, value);
  642.       else
  643.         value = convert_to_mode (mode, value, 1);
  644.     }
  645.  
  646.       if (must_and)
  647.     value = expand_binop (mode, and_optab, value,
  648.                   mask_rtx (mode, 0, bitsize, 0),
  649.                   NULL_RTX, 1, OPTAB_LIB_WIDEN);
  650.       if (bitpos > 0)
  651.     value = expand_shift (LSHIFT_EXPR, mode, value,
  652.                   build_int_2 (bitpos, 0), NULL_RTX, 1);
  653.     }
  654.  
  655.   /* Now clear the chosen bits in OP0,
  656.      except that if VALUE is -1 we need not bother.  */
  657.  
  658.   subtarget = (GET_CODE (op0) == REG || ! flag_force_mem) ? op0 : 0;
  659.  
  660.   if (! all_one)
  661.     {
  662.       temp = expand_binop (mode, and_optab, op0,
  663.                mask_rtx (mode, bitpos, bitsize, 1),
  664.                subtarget, 1, OPTAB_LIB_WIDEN);
  665.       subtarget = temp;
  666.     }
  667.   else
  668.     temp = op0;
  669.  
  670.   /* Now logical-or VALUE into OP0, unless it is zero.  */
  671.  
  672.   if (! all_zero)
  673.     temp = expand_binop (mode, ior_optab, temp, value,
  674.              subtarget, 1, OPTAB_LIB_WIDEN);
  675.   if (op0 != temp)
  676.     emit_move_insn (op0, temp);
  677. }
  678.  
  679. /* Store a bit field that is split across multiple accessible memory objects.
  680.  
  681.    OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
  682.    BITSIZE is the field width; BITPOS the position of its first bit
  683.    (within the word).
  684.    VALUE is the value to store.
  685.    ALIGN is the known alignment of OP0, measured in bytes.
  686.    This is also the size of the memory objects to be used.
  687.  
  688.    This does not yet handle fields wider than BITS_PER_WORD.  */
  689.  
  690. static void
  691. store_split_bit_field (op0, bitsize, bitpos, value, align)
  692.      rtx op0;
  693.      int bitsize, bitpos;
  694.      rtx value;
  695.      int align;
  696. {
  697.   int unit;
  698.   int bitsdone = 0;
  699.  
  700.   /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
  701.      much at a time.  */
  702.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  703.     unit = BITS_PER_WORD;
  704.   else
  705.     unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
  706.  
  707.   /* If VALUE is a constant other than a CONST_INT, get it into a register in
  708.      WORD_MODE.  If we can do this using gen_lowpart_common, do so.  Note
  709.      that VALUE might be a floating-point constant.  */
  710.   if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
  711.     {
  712.       rtx word = gen_lowpart_common (word_mode, value);
  713.  
  714.       if (word && (value != word))
  715.     value = word;
  716.       else
  717.     value = gen_lowpart_common (word_mode,
  718.                     force_reg (GET_MODE (value), value));
  719.     }
  720.  
  721.   while (bitsdone < bitsize)
  722.     {
  723.       int thissize;
  724.       rtx part, word;
  725.       int thispos;
  726.       int offset;
  727.  
  728.       offset = (bitpos + bitsdone) / unit;
  729.       thispos = (bitpos + bitsdone) % unit;
  730.  
  731.       /* THISSIZE must not overrun a word boundary.  Otherwise,
  732.      store_fixed_bit_field will call us again, and we will mutually
  733.      recurse forever.  */
  734.       thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
  735.       thissize = MIN (thissize, unit - thispos);
  736.  
  737. #if BYTES_BIG_ENDIAN
  738.       /* Fetch successively less significant portions.  */
  739.       if (GET_CODE (value) == CONST_INT)
  740.     part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
  741.              >> (bitsize - bitsdone - thissize))
  742.             & (((HOST_WIDE_INT) 1 << thissize) - 1));
  743.       else
  744.     {
  745.       /* The args are chosen so that the last part
  746.          includes the lsb.  */
  747.       int bit_offset = 0;
  748.       /* If the value isn't in memory, then it must be right aligned
  749.          if a register, so skip past the padding on the left.  If it
  750.          is in memory, then there is no padding on the left.  */
  751.       if (GET_CODE (value) != MEM)
  752.         bit_offset = BITS_PER_WORD - bitsize;
  753.       part = extract_fixed_bit_field (word_mode, value, 0, thissize,
  754.                       bit_offset + bitsdone,
  755.                       NULL_RTX, 1, align);
  756.     }
  757. #else
  758.       /* Fetch successively more significant portions.  */
  759.       if (GET_CODE (value) == CONST_INT)
  760.     part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value)) >> bitsdone)
  761.             & (((HOST_WIDE_INT) 1 << thissize) - 1));
  762.       else
  763.     part = extract_fixed_bit_field (word_mode, value, 0, thissize,
  764.                     bitsdone, NULL_RTX, 1, align);
  765. #endif
  766.  
  767.       /* If OP0 is a register, then handle OFFSET here.
  768.  
  769.      When handling multiword bitfields, extract_bit_field may pass
  770.      down a word_mode SUBREG of a larger REG for a bitfield that actually
  771.      crosses a word boundary.  Thus, for a SUBREG, we must find
  772.      the current word starting from the base register.  */
  773.       if (GET_CODE (op0) == SUBREG)
  774.     {
  775.       word = operand_subword_force (SUBREG_REG (op0),
  776.                     SUBREG_WORD (op0) + offset,
  777.                     GET_MODE (SUBREG_REG (op0)));
  778.       offset = 0;
  779.     }
  780.       else if (GET_CODE (op0) == REG)
  781.     {
  782.       word = operand_subword_force (op0, offset, GET_MODE (op0));
  783.       offset = 0;
  784.     }
  785.       else
  786.     word = op0;
  787.  
  788.       /* OFFSET is in UNITs, and UNIT is in bits.
  789.          store_fixed_bit_field wants offset in bytes.  */
  790.       store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT,
  791.                  thissize, thispos, part, align);
  792.       bitsdone += thissize;
  793.     }
  794. }
  795.  
  796. /* Generate code to extract a byte-field from STR_RTX
  797.    containing BITSIZE bits, starting at BITNUM,
  798.    and put it in TARGET if possible (if TARGET is nonzero).
  799.    Regardless of TARGET, we return the rtx for where the value is placed.
  800.    It may be a QUEUED.
  801.  
  802.    STR_RTX is the structure containing the byte (a REG or MEM).
  803.    UNSIGNEDP is nonzero if this is an unsigned bit field.
  804.    MODE is the natural mode of the field value once extracted.
  805.    TMODE is the mode the caller would like the value to have;
  806.    but the value may be returned with type MODE instead.
  807.  
  808.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
  809.    TOTAL_SIZE is the size in bytes of the containing structure,
  810.    or -1 if varying.
  811.  
  812.    If a TARGET is specified and we can store in it at no extra cost,
  813.    we do so, and return TARGET.
  814.    Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
  815.    if they are equally easy.  */
  816.  
  817. rtx
  818. extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
  819.            target, mode, tmode, align, total_size)
  820.      rtx str_rtx;
  821.      register int bitsize;
  822.      int bitnum;
  823.      int unsignedp;
  824.      rtx target;
  825.      enum machine_mode mode, tmode;
  826.      int align;
  827.      int total_size;
  828. {
  829.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  830.   register int offset = bitnum / unit;
  831.   register int bitpos = bitnum % unit;
  832.   register rtx op0 = str_rtx;
  833.   rtx spec_target = target;
  834.   rtx spec_target_subreg = 0;
  835.  
  836.   if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
  837.     abort ();
  838.  
  839.   /* Discount the part of the structure before the desired byte.
  840.      We need to know how many bytes are safe to reference after it.  */
  841.   if (total_size >= 0)
  842.     total_size -= (bitpos / BIGGEST_ALIGNMENT
  843.            * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  844.  
  845.   if (tmode == VOIDmode)
  846.     tmode = mode;
  847.   while (GET_CODE (op0) == SUBREG)
  848.     {
  849.       offset += SUBREG_WORD (op0);
  850.       op0 = SUBREG_REG (op0);
  851.     }
  852.   
  853. #if BYTES_BIG_ENDIAN
  854.   /* If OP0 is a register, BITPOS must count within a word.
  855.      But as we have it, it counts within whatever size OP0 now has.
  856.      On a bigendian machine, these are not the same, so convert.  */
  857.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  858.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  859. #endif
  860.  
  861.   /* Extracting a full-word or multi-word value
  862.      from a structure in a register or aligned memory.
  863.      This can be done with just SUBREG.
  864.      So too extracting a subword value in
  865.      the least significant part of the register.  */
  866.  
  867.   if ((GET_CODE (op0) == REG
  868.        || (GET_CODE (op0) == MEM
  869.        && (! SLOW_UNALIGNED_ACCESS
  870.            || (offset * BITS_PER_UNIT % bitsize == 0
  871.            && align * BITS_PER_UNIT % bitsize == 0))))
  872.       && ((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
  873.        && bitpos % BITS_PER_WORD == 0)
  874.       || (mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0) != BLKmode
  875. #if BYTES_BIG_ENDIAN
  876.           && bitpos + bitsize == BITS_PER_WORD
  877. #else
  878.           && bitpos == 0
  879. #endif
  880.           )))
  881.     {
  882.       enum machine_mode mode1
  883.     = mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0);
  884.  
  885.       if (mode1 != GET_MODE (op0))
  886.     {
  887.       if (GET_CODE (op0) == REG)
  888.         op0 = gen_rtx (SUBREG, mode1, op0, offset);
  889.       else
  890.         op0 = change_address (op0, mode1,
  891.                   plus_constant (XEXP (op0, 0), offset));
  892.     }
  893.       if (mode1 != mode)
  894.     return convert_to_mode (tmode, op0, unsignedp);
  895.       return op0;
  896.     }
  897.  
  898.   /* Handle fields bigger than a word.  */
  899.   
  900.   if (bitsize > BITS_PER_WORD)
  901.     {
  902.       /* Here we transfer the words of the field
  903.      in the order least significant first.
  904.      This is because the most significant word is the one which may
  905.      be less than full.  */
  906.  
  907.       int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
  908.       int i;
  909.  
  910.       if (target == 0 || GET_CODE (target) != REG)
  911.     target = gen_reg_rtx (mode);
  912.  
  913.       for (i = 0; i < nwords; i++)
  914.     {
  915.       /* If I is 0, use the low-order word in both field and target;
  916.          if I is 1, use the next to lowest word; and so on.  */
  917.       int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
  918.       int bit_offset = (WORDS_BIG_ENDIAN
  919.                 ? MAX (0, bitsize - (i + 1) * BITS_PER_WORD)
  920.                 : i * BITS_PER_WORD);
  921.       rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
  922.       rtx result_part
  923.         = extract_bit_field (op0, MIN (BITS_PER_WORD,
  924.                        bitsize - i * BITS_PER_WORD),
  925.                  bitnum + bit_offset,
  926.                  1, target_part, mode, word_mode,
  927.                  align, total_size);
  928.  
  929.       if (target_part == 0)
  930.         abort ();
  931.  
  932.       if (result_part != target_part)
  933.         emit_move_insn (target_part, result_part);
  934.     }
  935.  
  936.       if (unsignedp)
  937.     return target;
  938.       /* Signed bit field: sign-extend with two arithmetic shifts.  */
  939.       target = expand_shift (LSHIFT_EXPR, mode, target,
  940.                  build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
  941.                  NULL_RTX, 0);
  942.       return expand_shift (RSHIFT_EXPR, mode, target,
  943.                build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
  944.                NULL_RTX, 0);
  945.     }
  946.   
  947.   /* From here on we know the desired field is smaller than a word
  948.      so we can assume it is an integer.  So we can safely extract it as one
  949.      size of integer, if necessary, and then truncate or extend
  950.      to the size that is wanted.  */
  951.  
  952.   /* OFFSET is the number of words or bytes (UNIT says which)
  953.      from STR_RTX to the first word or byte containing part of the field.  */
  954.  
  955.   if (GET_CODE (op0) == REG)
  956.     {
  957.       if (offset != 0
  958.       || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
  959.     op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
  960.                op0, offset);
  961.       offset = 0;
  962.     }
  963.   else
  964.     {
  965.       op0 = protect_from_queue (str_rtx, 1);
  966.     }
  967.  
  968.   /* Now OFFSET is nonzero only for memory operands.  */
  969.  
  970.   if (unsignedp)
  971.     {
  972. #ifdef HAVE_extzv
  973.       if (HAVE_extzv
  974.       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extzv][0])
  975.           >= bitsize))
  976.     {
  977.       int xbitpos = bitpos, xoffset = offset;
  978.       rtx bitsize_rtx, bitpos_rtx;
  979.       rtx last = get_last_insn();
  980.       rtx xop0 = op0;
  981.       rtx xtarget = target;
  982.       rtx xspec_target = spec_target;
  983.       rtx xspec_target_subreg = spec_target_subreg;
  984.       rtx pat;
  985.       enum machine_mode maxmode
  986.         = insn_operand_mode[(int) CODE_FOR_extzv][0];
  987.  
  988.       if (GET_CODE (xop0) == MEM)
  989.         {
  990.           int save_volatile_ok = volatile_ok;
  991.           volatile_ok = 1;
  992.  
  993.           /* Is the memory operand acceptable?  */
  994.           if (flag_force_mem
  995.           || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  996.             (xop0, GET_MODE (xop0))))
  997.         {
  998.           /* No, load into a reg and extract from there.  */
  999.           enum machine_mode bestmode;
  1000.  
  1001.           /* Get the mode to use for inserting into this field.  If
  1002.              OP0 is BLKmode, get the smallest mode consistent with the
  1003.              alignment. If OP0 is a non-BLKmode object that is no
  1004.              wider than MAXMODE, use its mode. Otherwise, use the
  1005.              smallest mode containing the field.  */
  1006.  
  1007.           if (GET_MODE (xop0) == BLKmode
  1008.               || (GET_MODE_SIZE (GET_MODE (op0))
  1009.               > GET_MODE_SIZE (maxmode)))
  1010.             bestmode = get_best_mode (bitsize, bitnum,
  1011.                           align * BITS_PER_UNIT, maxmode,
  1012.                           MEM_VOLATILE_P (xop0));
  1013.           else
  1014.             bestmode = GET_MODE (xop0);
  1015.  
  1016.           if (bestmode == VOIDmode
  1017.               || (STRICT_ALIGNMENT && GET_MODE_SIZE (bestmode) > align))
  1018.             goto extzv_loses;
  1019.  
  1020.           /* Compute offset as multiple of this unit,
  1021.              counting in bytes.  */
  1022.           unit = GET_MODE_BITSIZE (bestmode);
  1023.           xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  1024.           xbitpos = bitnum % unit;
  1025.           xop0 = change_address (xop0, bestmode,
  1026.                      plus_constant (XEXP (xop0, 0),
  1027.                             xoffset));
  1028.           /* Fetch it to a register in that size.  */
  1029.           xop0 = force_reg (bestmode, xop0);
  1030.  
  1031.           /* XBITPOS counts within UNIT, which is what is expected.  */
  1032.         }
  1033.           else
  1034.         /* Get ref to first byte containing part of the field.  */
  1035.         xop0 = change_address (xop0, byte_mode,
  1036.                        plus_constant (XEXP (xop0, 0), xoffset));
  1037.  
  1038.           volatile_ok = save_volatile_ok;
  1039.         }
  1040.  
  1041.       /* If op0 is a register, we need it in MAXMODE (which is usually
  1042.          SImode). to make it acceptable to the format of extzv.  */
  1043.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
  1044.         abort ();
  1045.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
  1046.         xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
  1047.  
  1048.       /* On big-endian machines, we count bits from the most significant.
  1049.          If the bit field insn does not, we must invert.  */
  1050. #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
  1051.       xbitpos = unit - bitsize - xbitpos;
  1052. #endif
  1053.       /* Now convert from counting within UNIT to counting in MAXMODE.  */
  1054. #if BITS_BIG_ENDIAN
  1055.       if (GET_CODE (xop0) != MEM)
  1056.         xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
  1057. #endif
  1058.       unit = GET_MODE_BITSIZE (maxmode);
  1059.  
  1060.       if (xtarget == 0
  1061.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  1062.         xtarget = xspec_target = gen_reg_rtx (tmode);
  1063.  
  1064.       if (GET_MODE (xtarget) != maxmode)
  1065.         {
  1066.           if (GET_CODE (xtarget) == REG)
  1067.         {
  1068.           int wider = (GET_MODE_SIZE (maxmode)
  1069.                    > GET_MODE_SIZE (GET_MODE (xtarget)));
  1070.           xtarget = gen_lowpart (maxmode, xtarget);
  1071.           if (wider)
  1072.             xspec_target_subreg = xtarget;
  1073.         }
  1074.           else
  1075.         xtarget = gen_reg_rtx (maxmode);
  1076.         }
  1077.  
  1078.       /* If this machine's extzv insists on a register target,
  1079.          make sure we have one.  */
  1080.       if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
  1081.          (xtarget, maxmode)))
  1082.         xtarget = gen_reg_rtx (maxmode);
  1083.  
  1084.       bitsize_rtx = GEN_INT (bitsize);
  1085.       bitpos_rtx = GEN_INT (xbitpos);
  1086.  
  1087.       pat = gen_extzv (protect_from_queue (xtarget, 1),
  1088.                xop0, bitsize_rtx, bitpos_rtx);
  1089.       if (pat)
  1090.         {
  1091.           emit_insn (pat);
  1092.           target = xtarget;
  1093.           spec_target = xspec_target;
  1094.           spec_target_subreg = xspec_target_subreg;
  1095.         }
  1096.       else
  1097.         {
  1098.           delete_insns_since (last);
  1099.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  1100.                         bitpos, target, 1, align);
  1101.         }
  1102.     }
  1103.       else
  1104.         extzv_loses:
  1105. #endif
  1106.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1107.                       target, 1, align);
  1108.     }
  1109.   else
  1110.     {
  1111. #ifdef HAVE_extv
  1112.       if (HAVE_extv
  1113.       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extv][0])
  1114.           >= bitsize))
  1115.     {
  1116.       int xbitpos = bitpos, xoffset = offset;
  1117.       rtx bitsize_rtx, bitpos_rtx;
  1118.       rtx last = get_last_insn();
  1119.       rtx xop0 = op0, xtarget = target;
  1120.       rtx xspec_target = spec_target;
  1121.       rtx xspec_target_subreg = spec_target_subreg;
  1122.       rtx pat;
  1123.       enum machine_mode maxmode
  1124.         = insn_operand_mode[(int) CODE_FOR_extv][0];
  1125.  
  1126.       if (GET_CODE (xop0) == MEM)
  1127.         {
  1128.           /* Is the memory operand acceptable?  */
  1129.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][1])
  1130.              (xop0, GET_MODE (xop0))))
  1131.         {
  1132.           /* No, load into a reg and extract from there.  */
  1133.           enum machine_mode bestmode;
  1134.  
  1135.           /* Get the mode to use for inserting into this field.  If
  1136.              OP0 is BLKmode, get the smallest mode consistent with the
  1137.              alignment. If OP0 is a non-BLKmode object that is no
  1138.              wider than MAXMODE, use its mode. Otherwise, use the
  1139.              smallest mode containing the field.  */
  1140.  
  1141.           if (GET_MODE (xop0) == BLKmode
  1142.               || (GET_MODE_SIZE (GET_MODE (op0))
  1143.               > GET_MODE_SIZE (maxmode)))
  1144.             bestmode = get_best_mode (bitsize, bitnum,
  1145.                           align * BITS_PER_UNIT, maxmode,
  1146.                           MEM_VOLATILE_P (xop0));
  1147.           else
  1148.             bestmode = GET_MODE (xop0);
  1149.  
  1150.           if (bestmode == VOIDmode
  1151.               || (STRICT_ALIGNMENT && GET_MODE_SIZE (bestmode) > align))
  1152.             goto extv_loses;
  1153.  
  1154.           /* Compute offset as multiple of this unit,
  1155.              counting in bytes.  */
  1156.           unit = GET_MODE_BITSIZE (bestmode);
  1157.           xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  1158.           xbitpos = bitnum % unit;
  1159.           xop0 = change_address (xop0, bestmode,
  1160.                      plus_constant (XEXP (xop0, 0),
  1161.                             xoffset));
  1162.           /* Fetch it to a register in that size.  */
  1163.           xop0 = force_reg (bestmode, xop0);
  1164.  
  1165.           /* XBITPOS counts within UNIT, which is what is expected.  */
  1166.         }
  1167.           else
  1168.         /* Get ref to first byte containing part of the field.  */
  1169.         xop0 = change_address (xop0, byte_mode,
  1170.                        plus_constant (XEXP (xop0, 0), xoffset));
  1171.         }
  1172.  
  1173.       /* If op0 is a register, we need it in MAXMODE (which is usually
  1174.          SImode) to make it acceptable to the format of extv.  */
  1175.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
  1176.         abort ();
  1177.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
  1178.         xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
  1179.  
  1180.       /* On big-endian machines, we count bits from the most significant.
  1181.          If the bit field insn does not, we must invert.  */
  1182. #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
  1183.       xbitpos = unit - bitsize - xbitpos;
  1184. #endif
  1185.       /* XBITPOS counts within a size of UNIT.
  1186.          Adjust to count within a size of MAXMODE.  */
  1187. #if BITS_BIG_ENDIAN
  1188.       if (GET_CODE (xop0) != MEM)
  1189.         xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
  1190. #endif
  1191.       unit = GET_MODE_BITSIZE (maxmode);
  1192.  
  1193.       if (xtarget == 0
  1194.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  1195.         xtarget = xspec_target = gen_reg_rtx (tmode);
  1196.  
  1197.       if (GET_MODE (xtarget) != maxmode)
  1198.         {
  1199.           if (GET_CODE (xtarget) == REG)
  1200.         {
  1201.           int wider = (GET_MODE_SIZE (maxmode)
  1202.                    > GET_MODE_SIZE (GET_MODE (xtarget)));
  1203.           xtarget = gen_lowpart (maxmode, xtarget);
  1204.           if (wider)
  1205.             xspec_target_subreg = xtarget;
  1206.         }
  1207.           else
  1208.         xtarget = gen_reg_rtx (maxmode);
  1209.         }
  1210.  
  1211.       /* If this machine's extv insists on a register target,
  1212.          make sure we have one.  */
  1213.       if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][0])
  1214.          (xtarget, maxmode)))
  1215.         xtarget = gen_reg_rtx (maxmode);
  1216.  
  1217.       bitsize_rtx = GEN_INT (bitsize);
  1218.       bitpos_rtx = GEN_INT (xbitpos);
  1219.  
  1220.       pat = gen_extv (protect_from_queue (xtarget, 1),
  1221.               xop0, bitsize_rtx, bitpos_rtx);
  1222.       if (pat)
  1223.         {
  1224.           emit_insn (pat);
  1225.           target = xtarget;
  1226.           spec_target = xspec_target;
  1227.           spec_target_subreg = xspec_target_subreg;
  1228.         }
  1229.       else
  1230.         {
  1231.           delete_insns_since (last);
  1232.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  1233.                         bitpos, target, 0, align);
  1234.         }
  1235.     } 
  1236.       else
  1237.     extv_loses:
  1238. #endif
  1239.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1240.                       target, 0, align);
  1241.     }
  1242.   if (target == spec_target)
  1243.     return target;
  1244.   if (target == spec_target_subreg)
  1245.     return spec_target;
  1246.   if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
  1247.     {
  1248.       /* If the target mode is floating-point, first convert to the
  1249.      integer mode of that size and then access it as a floating-point
  1250.      value via a SUBREG.  */
  1251.       if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
  1252.     {
  1253.       target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
  1254.                            MODE_INT, 0),
  1255.                     target, unsignedp);
  1256.       if (GET_CODE (target) != REG)
  1257.         target = copy_to_reg (target);
  1258.       return gen_rtx (SUBREG, tmode, target, 0);
  1259.     }
  1260.       else
  1261.     return convert_to_mode (tmode, target, unsignedp);
  1262.     }
  1263.   return target;
  1264. }
  1265.  
  1266. /* Extract a bit field using shifts and boolean operations
  1267.    Returns an rtx to represent the value.
  1268.    OP0 addresses a register (word) or memory (byte).
  1269.    BITPOS says which bit within the word or byte the bit field starts in.
  1270.    OFFSET says how many bytes farther the bit field starts;
  1271.     it is 0 if OP0 is a register.
  1272.    BITSIZE says how many bits long the bit field is.
  1273.     (If OP0 is a register, it may be narrower than a full word,
  1274.      but BITPOS still counts within a full word,
  1275.      which is significant on bigendian machines.)
  1276.  
  1277.    UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
  1278.    If TARGET is nonzero, attempts to store the value there
  1279.    and return TARGET, but this is not guaranteed.
  1280.    If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
  1281.  
  1282.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.  */
  1283.  
  1284. static rtx
  1285. extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1286.              target, unsignedp, align)
  1287.      enum machine_mode tmode;
  1288.      register rtx op0, target;
  1289.      register int offset, bitsize, bitpos;
  1290.      int unsignedp;
  1291.      int align;
  1292. {
  1293.   int total_bits = BITS_PER_WORD;
  1294.   enum machine_mode mode;
  1295.  
  1296.   if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
  1297.     {
  1298.       /* Special treatment for a bit field split across two registers.  */
  1299.       if (bitsize + bitpos > BITS_PER_WORD)
  1300.     return extract_split_bit_field (op0, bitsize, bitpos,
  1301.                     unsignedp, align);
  1302.     }
  1303.   else
  1304.     {
  1305.       /* Get the proper mode to use for this field.  We want a mode that
  1306.      includes the entire field.  If such a mode would be larger than
  1307.      a word, we won't be doing the extraction the normal way.  */
  1308.  
  1309.       mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
  1310.                 align * BITS_PER_UNIT, word_mode,
  1311.                 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
  1312.  
  1313.       if (mode == VOIDmode)
  1314.     /* The only way this should occur is if the field spans word
  1315.        boundaries.  */
  1316.     return extract_split_bit_field (op0, bitsize,
  1317.                     bitpos + offset * BITS_PER_UNIT,
  1318.                     unsignedp, align);
  1319.  
  1320.       total_bits = GET_MODE_BITSIZE (mode);
  1321.  
  1322.       /* Make sure bitpos is valid for the chosen mode.  Adjust BITPOS to
  1323.      be be in the range 0 to total_bits-1, and put any excess bytes in
  1324.      OFFSET.  */
  1325.       if (bitpos >= total_bits)
  1326.     {
  1327.       offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
  1328.       bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
  1329.              * BITS_PER_UNIT);
  1330.     }
  1331.  
  1332.       /* Get ref to an aligned byte, halfword, or word containing the field.
  1333.      Adjust BITPOS to be position within a word,
  1334.      and OFFSET to be the offset of that word.
  1335.      Then alter OP0 to refer to that word.  */
  1336.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  1337.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  1338.       op0 = change_address (op0, mode,
  1339.                 plus_constant (XEXP (op0, 0), offset));
  1340.     }
  1341.  
  1342.   mode = GET_MODE (op0);
  1343.  
  1344. #if BYTES_BIG_ENDIAN
  1345.   /* BITPOS is the distance between our msb and that of OP0.
  1346.      Convert it to the distance from the lsb.  */
  1347.  
  1348.   bitpos = total_bits - bitsize - bitpos;
  1349. #endif
  1350.   /* Now BITPOS is always the distance between the field's lsb and that of OP0.
  1351.      We have reduced the big-endian case to the little-endian case.  */
  1352.  
  1353.   if (unsignedp)
  1354.     {
  1355.       if (bitpos)
  1356.     {
  1357.       /* If the field does not already start at the lsb,
  1358.          shift it so it does.  */
  1359.       tree amount = build_int_2 (bitpos, 0);
  1360.       /* Maybe propagate the target for the shift.  */
  1361.       /* But not if we will return it--could confuse integrate.c.  */
  1362.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  1363.                && !REG_FUNCTION_VALUE_P (target)
  1364.                ? target : 0);
  1365.       if (tmode != mode) subtarget = 0;
  1366.       op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  1367.     }
  1368.       /* Convert the value to the desired mode.  */
  1369.       if (mode != tmode)
  1370.     op0 = convert_to_mode (tmode, op0, 1);
  1371.  
  1372.       /* Unless the msb of the field used to be the msb when we shifted,
  1373.      mask out the upper bits.  */
  1374.  
  1375.       if (GET_MODE_BITSIZE (mode) != bitpos + bitsize
  1376. #if 0
  1377. #ifdef SLOW_ZERO_EXTEND
  1378.       /* Always generate an `and' if
  1379.          we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
  1380.          will combine fruitfully with the zero-extend. */
  1381.       || tmode != mode
  1382. #endif
  1383. #endif
  1384.       )
  1385.     return expand_binop (GET_MODE (op0), and_optab, op0,
  1386.                  mask_rtx (GET_MODE (op0), 0, bitsize, 0),
  1387.                  target, 1, OPTAB_LIB_WIDEN);
  1388.       return op0;
  1389.     }
  1390.  
  1391.   /* To extract a signed bit-field, first shift its msb to the msb of the word,
  1392.      then arithmetic-shift its lsb to the lsb of the word.  */
  1393.   op0 = force_reg (mode, op0);
  1394.   if (mode != tmode)
  1395.     target = 0;
  1396.  
  1397.   /* Find the narrowest integer mode that contains the field.  */
  1398.  
  1399.   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
  1400.        mode = GET_MODE_WIDER_MODE (mode))
  1401.     if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
  1402.       {
  1403.     op0 = convert_to_mode (mode, op0, 0);
  1404.     break;
  1405.       }
  1406.  
  1407.   if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
  1408.     {
  1409.       tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
  1410.       /* Maybe propagate the target for the shift.  */
  1411.       /* But not if we will return the result--could confuse integrate.c.  */
  1412.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  1413.                && ! REG_FUNCTION_VALUE_P (target)
  1414.                ? target : 0);
  1415.       op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  1416.     }
  1417.  
  1418.   return expand_shift (RSHIFT_EXPR, mode, op0,
  1419.                build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0), 
  1420.                target, 0);
  1421. }
  1422.  
  1423. /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
  1424.    of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
  1425.    complement of that if COMPLEMENT.  The mask is truncated if
  1426.    necessary to the width of mode MODE.  */
  1427.  
  1428. static rtx
  1429. mask_rtx (mode, bitpos, bitsize, complement)
  1430.      enum machine_mode mode;
  1431.      int bitpos, bitsize, complement;
  1432. {
  1433.   HOST_WIDE_INT masklow, maskhigh;
  1434.  
  1435.   if (bitpos < HOST_BITS_PER_WIDE_INT)
  1436.     masklow = (HOST_WIDE_INT) -1 << bitpos;
  1437.   else
  1438.     masklow = 0;
  1439.  
  1440.   if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
  1441.     masklow &= ((unsigned HOST_WIDE_INT) -1
  1442.         >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
  1443.   
  1444.   if (bitpos <= HOST_BITS_PER_WIDE_INT)
  1445.     maskhigh = -1;
  1446.   else
  1447.     maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
  1448.  
  1449.   if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
  1450.     maskhigh &= ((unsigned HOST_WIDE_INT) -1
  1451.          >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
  1452.   else
  1453.     maskhigh = 0;
  1454.  
  1455.   if (complement)
  1456.     {
  1457.       maskhigh = ~maskhigh;
  1458.       masklow = ~masklow;
  1459.     }
  1460.  
  1461.   return immed_double_const (masklow, maskhigh, mode);
  1462. }
  1463.  
  1464. /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
  1465.    VALUE truncated to BITSIZE bits and then shifted left BITPOS bits.  */
  1466.  
  1467. static rtx
  1468. lshift_value (mode, value, bitpos, bitsize)
  1469.      enum machine_mode mode;
  1470.      rtx value;
  1471.      int bitpos, bitsize;
  1472. {
  1473.   unsigned HOST_WIDE_INT v = INTVAL (value);
  1474.   HOST_WIDE_INT low, high;
  1475.  
  1476.   if (bitsize < HOST_BITS_PER_WIDE_INT)
  1477.     v &= ~((HOST_WIDE_INT) -1 << bitsize);
  1478.  
  1479.   if (bitpos < HOST_BITS_PER_WIDE_INT)
  1480.     {
  1481.       low = v << bitpos;
  1482.       high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
  1483.     }
  1484.   else
  1485.     {
  1486.       low = 0;
  1487.       high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
  1488.     }
  1489.  
  1490.   return immed_double_const (low, high, mode);
  1491. }
  1492.  
  1493. /* Extract a bit field that is split across two words
  1494.    and return an RTX for the result.
  1495.  
  1496.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  1497.    BITSIZE is the field width; BITPOS, position of its first bit, in the word.
  1498.    UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.
  1499.  
  1500.    ALIGN is the known alignment of OP0, measured in bytes.
  1501.    This is also the size of the memory objects to be used.  */
  1502.  
  1503. static rtx
  1504. extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
  1505.      rtx op0;
  1506.      int bitsize, bitpos, unsignedp, align;
  1507. {
  1508.   int unit;
  1509.   int bitsdone = 0;
  1510.   rtx result;
  1511.   int first = 1;
  1512.  
  1513.   /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
  1514.      much at a time.  */
  1515.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  1516.     unit = BITS_PER_WORD;
  1517.   else
  1518.     unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
  1519.  
  1520.   while (bitsdone < bitsize)
  1521.     {
  1522.       int thissize;
  1523.       rtx part, word;
  1524.       int thispos;
  1525.       int offset;
  1526.  
  1527.       offset = (bitpos + bitsdone) / unit;
  1528.       thispos = (bitpos + bitsdone) % unit;
  1529.  
  1530.       /* THISSIZE must not overrun a word boundary.  Otherwise,
  1531.      extract_fixed_bit_field will call us again, and we will mutually
  1532.      recurse forever.  */
  1533.       thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
  1534.       thissize = MIN (thissize, unit - thispos);
  1535.  
  1536.       /* If OP0 is a register, then handle OFFSET here.
  1537.  
  1538.      When handling multiword bitfields, extract_bit_field may pass
  1539.      down a word_mode SUBREG of a larger REG for a bitfield that actually
  1540.      crosses a word boundary.  Thus, for a SUBREG, we must find
  1541.      the current word starting from the base register.  */
  1542.       if (GET_CODE (op0) == SUBREG)
  1543.     {
  1544.       word = operand_subword_force (SUBREG_REG (op0),
  1545.                     SUBREG_WORD (op0) + offset,
  1546.                     GET_MODE (SUBREG_REG (op0)));
  1547.       offset = 0;
  1548.     }
  1549.       else if (GET_CODE (op0) == REG)
  1550.     {
  1551.       word = operand_subword_force (op0, offset, GET_MODE (op0));
  1552.       offset = 0;
  1553.     }
  1554.       else
  1555.     word = op0;
  1556.  
  1557.       /* Extract the parts in bit-counting order,
  1558.      whose meaning is determined by BYTES_PER_UNIT.
  1559.      OFFSET is in UNITs, and UNIT is in bits.
  1560.      extract_fixed_bit_field wants offset in bytes.  */
  1561.       part = extract_fixed_bit_field (word_mode, word,
  1562.                       offset * unit / BITS_PER_UNIT,
  1563.                       thissize, thispos, 0, 1, align);
  1564.       bitsdone += thissize;
  1565.  
  1566.       /* Shift this part into place for the result.  */
  1567. #if BYTES_BIG_ENDIAN
  1568.       if (bitsize != bitsdone)
  1569.     part = expand_shift (LSHIFT_EXPR, word_mode, part,
  1570.                  build_int_2 (bitsize - bitsdone, 0), 0, 1);
  1571. #else
  1572.       if (bitsdone != thissize)
  1573.     part = expand_shift (LSHIFT_EXPR, word_mode, part,
  1574.                  build_int_2 (bitsdone - thissize, 0), 0, 1);
  1575. #endif
  1576.  
  1577.       if (first)
  1578.     result = part;
  1579.       else
  1580.     /* Combine the parts with bitwise or.  This works
  1581.        because we extracted each part as an unsigned bit field.  */
  1582.     result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
  1583.                    OPTAB_LIB_WIDEN);
  1584.  
  1585.       first = 0;
  1586.     }
  1587.  
  1588.   /* Unsigned bit field: we are done.  */
  1589.   if (unsignedp)
  1590.     return result;
  1591.   /* Signed bit field: sign-extend with two arithmetic shifts.  */
  1592.   result = expand_shift (LSHIFT_EXPR, word_mode, result,
  1593.              build_int_2 (BITS_PER_WORD - bitsize, 0),
  1594.              NULL_RTX, 0);
  1595.   return expand_shift (RSHIFT_EXPR, word_mode, result,
  1596.                build_int_2 (BITS_PER_WORD - bitsize, 0), NULL_RTX, 0);
  1597. }
  1598.  
  1599. /* Add INC into TARGET.  */
  1600.  
  1601. void
  1602. expand_inc (target, inc)
  1603.      rtx target, inc;
  1604. {
  1605.   rtx value = expand_binop (GET_MODE (target), add_optab,
  1606.                 target, inc,
  1607.                 target, 0, OPTAB_LIB_WIDEN);
  1608.   if (value != target)
  1609.     emit_move_insn (target, value);
  1610. }
  1611.  
  1612. /* Subtract DEC from TARGET.  */
  1613.  
  1614. void
  1615. expand_dec (target, dec)
  1616.      rtx target, dec;
  1617. {
  1618.   rtx value = expand_binop (GET_MODE (target), sub_optab,
  1619.                 target, dec,
  1620.                 target, 0, OPTAB_LIB_WIDEN);
  1621.   if (value != target)
  1622.     emit_move_insn (target, value);
  1623. }
  1624.  
  1625. /* Output a shift instruction for expression code CODE,
  1626.    with SHIFTED being the rtx for the value to shift,
  1627.    and AMOUNT the tree for the amount to shift by.
  1628.    Store the result in the rtx TARGET, if that is convenient.
  1629.    If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  1630.    Return the rtx for where the value is.  */
  1631.  
  1632. rtx
  1633. expand_shift (code, mode, shifted, amount, target, unsignedp)
  1634.      enum tree_code code;
  1635.      register enum machine_mode mode;
  1636.      rtx shifted;
  1637.      tree amount;
  1638.      register rtx target;
  1639.      int unsignedp;
  1640. {
  1641.   register rtx op1, temp = 0;
  1642.   register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
  1643.   register int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
  1644.   int try;
  1645.  
  1646.   /* Previously detected shift-counts computed by NEGATE_EXPR
  1647.      and shifted in the other direction; but that does not work
  1648.      on all machines.  */
  1649.  
  1650.   op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
  1651.  
  1652. #if 0 && SHIFT_COUNT_TRUNCATED
  1653.   if (SHIFT_COUNT_TRUNCATED
  1654.       && GET_CODE (op1) == CONST_INT
  1655.       && (unsigned HOST_WIDE_INT) INTVAL (op1) >= GET_MODE_BITSIZE (mode))
  1656.     op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
  1657.            % GET_MODE_BITSIZE (mode));
  1658. #endif
  1659.  
  1660.   if (op1 == const0_rtx)
  1661.     return shifted;
  1662.  
  1663.   for (try = 0; temp == 0 && try < 3; try++)
  1664.     {
  1665.       enum optab_methods methods;
  1666.  
  1667.       if (try == 0)
  1668.     methods = OPTAB_DIRECT;
  1669.       else if (try == 1)
  1670.     methods = OPTAB_WIDEN;
  1671.       else
  1672.     methods = OPTAB_LIB_WIDEN;
  1673.  
  1674.       if (rotate)
  1675.     {
  1676.       /* Widening does not work for rotation.  */
  1677.       if (methods == OPTAB_WIDEN)
  1678.         continue;
  1679.       else if (methods == OPTAB_LIB_WIDEN)
  1680.         {
  1681.           /* If we are rotating by a constant that is valid and
  1682.          we have been unable to open-code this by a rotation,
  1683.          do it as the IOR of two shifts.  I.e., to rotate A
  1684.          by N bits, compute (A << N) | ((unsigned) A >> (C - N))
  1685.          where C is the bitsize of A.
  1686.  
  1687.          It is theoretically possible that the target machine might
  1688.          not be able to perform either shift and hence we would
  1689.          be making two libcalls rather than just the one for the
  1690.          shift (similarly if IOR could not be done).  We will allow
  1691.          this extremely unlikely lossage to avoid complicating the
  1692.          code below.  */
  1693.  
  1694.           if (GET_CODE (op1) == CONST_INT && INTVAL (op1) > 0
  1695.           && INTVAL (op1) < GET_MODE_BITSIZE (mode))
  1696.         {
  1697.           rtx subtarget = target == shifted ? 0 : target;
  1698.           rtx temp1;
  1699.           tree other_amount
  1700.             = build_int_2 (GET_MODE_BITSIZE (mode) - INTVAL (op1), 0);
  1701.  
  1702.           shifted = force_reg (mode, shifted);
  1703.  
  1704.           temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
  1705.                        mode, shifted, amount, subtarget, 1);
  1706.           temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
  1707.                     mode, shifted, other_amount, 0, 1);
  1708.           return expand_binop (mode, ior_optab, temp, temp1, target,
  1709.                        unsignedp, methods);
  1710.         }
  1711.           else
  1712.         methods = OPTAB_LIB;
  1713.         }
  1714.  
  1715.       temp = expand_binop (mode,
  1716.                    left ? rotl_optab : rotr_optab,
  1717.                    shifted, op1, target, unsignedp, methods);
  1718.  
  1719.       /* If we don't have the rotate, but we are rotating by a constant
  1720.          that is in range, try a rotate in the opposite direction.  */
  1721.  
  1722.       if (temp == 0 && GET_CODE (op1) == CONST_INT
  1723.           && INTVAL (op1) > 0 && INTVAL (op1) < GET_MODE_BITSIZE (mode))
  1724.         temp = expand_binop (mode,
  1725.                  left ? rotr_optab : rotl_optab,
  1726.                  shifted, 
  1727.                  GEN_INT (GET_MODE_BITSIZE (mode)
  1728.                       - INTVAL (op1)),
  1729.                  target, unsignedp, methods);
  1730.     }
  1731.       else if (unsignedp)
  1732.     temp = expand_binop (mode,
  1733.                  left ? ashl_optab : lshr_optab,
  1734.                  shifted, op1, target, unsignedp, methods);
  1735.  
  1736.       /* Do arithmetic shifts.
  1737.      Also, if we are going to widen the operand, we can just as well
  1738.      use an arithmetic right-shift instead of a logical one.  */
  1739.       if (temp == 0 && ! rotate
  1740.       && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
  1741.     {
  1742.       enum optab_methods methods1 = methods;
  1743.  
  1744.       /* If trying to widen a log shift to an arithmetic shift,
  1745.          don't accept an arithmetic shift of the same size.  */
  1746.       if (unsignedp)
  1747.         methods1 = OPTAB_MUST_WIDEN;
  1748.  
  1749.       /* Arithmetic shift */
  1750.  
  1751.       temp = expand_binop (mode,
  1752.                    left ? ashl_optab : ashr_optab,
  1753.                    shifted, op1, target, unsignedp, methods1);
  1754.     }
  1755.  
  1756.       /* We used to try extzv here for logical right shifts, but that was
  1757.      only useful for one machine, the VAX, and caused poor code 
  1758.      generation there for lshrdi3, so the code was deleted and a
  1759.      define_expand for lshrsi3 was added to vax.md.  */
  1760.     }
  1761.  
  1762.   if (temp == 0)
  1763.     abort ();
  1764.   return temp;
  1765. }
  1766.  
  1767. enum alg_code { alg_zero, alg_m, alg_shift,
  1768.           alg_add_t_m2, alg_sub_t_m2,
  1769.           alg_add_factor, alg_sub_factor,
  1770.           alg_add_t2_m, alg_sub_t2_m,
  1771.           alg_add, alg_subtract, alg_factor, alg_shiftop };
  1772.  
  1773. /* This structure records a sequence of operations.
  1774.    `ops' is the number of operations recorded.
  1775.    `cost' is their total cost.
  1776.    The operations are stored in `op' and the corresponding
  1777.    logarithms of the integer coefficients in `log'.
  1778.  
  1779.    These are the operations:
  1780.    alg_zero        total := 0;
  1781.    alg_m        total := multiplicand;
  1782.    alg_shift        total := total * coeff
  1783.    alg_add_t_m2        total := total + multiplicand * coeff;
  1784.    alg_sub_t_m2        total := total - multiplicand * coeff;
  1785.    alg_add_factor    total := total * coeff + total;
  1786.    alg_sub_factor    total := total * coeff - total;
  1787.    alg_add_t2_m        total := total * coeff + multiplicand;
  1788.    alg_sub_t2_m        total := total * coeff - multiplicand;
  1789.  
  1790.    The first operand must be either alg_zero or alg_m.  */
  1791.  
  1792. struct algorithm
  1793. {
  1794.   short cost;
  1795.   short ops;
  1796.   /* The size of the OP and LOG fields are not directly related to the
  1797.      word size, but the worst-case algorithms will be if we have few
  1798.      consecutive ones or zeros, i.e., a multiplicand like 10101010101...
  1799.      In that case we will generate shift-by-2, add, shift-by-2, add,...,
  1800.      in total wordsize operations.  */
  1801.   enum alg_code op[MAX_BITS_PER_WORD];
  1802.   char log[MAX_BITS_PER_WORD];
  1803. };
  1804.  
  1805. /* Compute and return the best algorithm for multiplying by T.
  1806.    The algorithm must cost less than cost_limit
  1807.    If retval.cost >= COST_LIMIT, no algorithm was found and all
  1808.    other field of the returned struct are undefined.  */
  1809.  
  1810. static void
  1811. synth_mult (alg_out, t, cost_limit)
  1812.      struct algorithm *alg_out;
  1813.      unsigned HOST_WIDE_INT t;
  1814.      int cost_limit;
  1815. {
  1816.   int m;
  1817.   struct algorithm *alg_in, *best_alg;
  1818.   unsigned int cost;
  1819.   unsigned HOST_WIDE_INT q;
  1820.  
  1821.   /* Indicate that no algorithm is yet found.  If no algorithm
  1822.      is found, this value will be returned and indicate failure.  */
  1823.   alg_out->cost = cost_limit;
  1824.  
  1825.   if (cost_limit <= 0)
  1826.     return;
  1827.  
  1828.   /* t == 1 can be done in zero cost.  */
  1829.   if (t == 1)
  1830.     {
  1831.       alg_out->ops = 1;
  1832.       alg_out->cost = 0;
  1833.       alg_out->op[0] = alg_m;
  1834.       return;
  1835.     }
  1836.  
  1837.   /* t == 0 sometimes has a cost.  If it does and it exceeds our limit,
  1838.      fail now.  */
  1839.   if (t == 0)
  1840.     {
  1841.       if (zero_cost >= cost_limit)
  1842.     return;
  1843.       else
  1844.     {
  1845.       alg_out->ops = 1;
  1846.       alg_out->cost = zero_cost;
  1847.       alg_out->op[0] = alg_zero;
  1848.       return;
  1849.     }
  1850.     }
  1851.  
  1852.   /* We'll be needing a couple extra algorithm structures now.  */
  1853.  
  1854.   alg_in = (struct algorithm *)alloca (sizeof (struct algorithm));
  1855.   best_alg = (struct algorithm *)alloca (sizeof (struct algorithm));
  1856.  
  1857.   /* If we have a group of zero bits at the low-order part of T, try
  1858.      multiplying by the remaining bits and then doing a shift.  */
  1859.  
  1860.   if ((t & 1) == 0)
  1861.     {
  1862.       m = floor_log2 (t & -t);    /* m = number of low zero bits */
  1863.       q = t >> m;
  1864.       cost = shift_cost[m];
  1865.       synth_mult (alg_in, q, cost_limit - cost);
  1866.  
  1867.       cost += alg_in->cost;
  1868.       if (cost < cost_limit)
  1869.     {
  1870.       struct algorithm *x;
  1871.       x = alg_in, alg_in = best_alg, best_alg = x;
  1872.       best_alg->log[best_alg->ops] = m;
  1873.       best_alg->op[best_alg->ops] = alg_shift;
  1874.       cost_limit = cost;
  1875.     }
  1876.     }
  1877.  
  1878.   /* If we have an odd number, add or subtract one.  */
  1879.   if ((t & 1) != 0)
  1880.     {
  1881.       unsigned HOST_WIDE_INT w;
  1882.  
  1883.       for (w = 1; (w & t) != 0; w <<= 1)
  1884.     ;
  1885.       if (w > 2
  1886.       /* Reject the case where t is 3.
  1887.          Thus we prefer addition in that case.  */
  1888.       && t != 3)
  1889.     {
  1890.       /* T ends with ...111.  Multiply by (T + 1) and subtract 1.  */
  1891.  
  1892.       cost = add_cost;
  1893.       synth_mult (alg_in, t + 1, cost_limit - cost);
  1894.  
  1895.       cost += alg_in->cost;
  1896.       if (cost < cost_limit)
  1897.         {
  1898.           struct algorithm *x;
  1899.           x = alg_in, alg_in = best_alg, best_alg = x;
  1900.           best_alg->log[best_alg->ops] = 0;
  1901.           best_alg->op[best_alg->ops] = alg_sub_t_m2;
  1902.           cost_limit = cost;
  1903.         }
  1904.     }
  1905.       else
  1906.     {
  1907.       /* T ends with ...01 or ...011.  Multiply by (T - 1) and add 1.  */
  1908.  
  1909.       cost = add_cost;
  1910.       synth_mult (alg_in, t - 1, cost_limit - cost);
  1911.  
  1912.       cost += alg_in->cost;
  1913.       if (cost < cost_limit)
  1914.         {
  1915.           struct algorithm *x;
  1916.           x = alg_in, alg_in = best_alg, best_alg = x;
  1917.           best_alg->log[best_alg->ops] = 0;
  1918.           best_alg->op[best_alg->ops] = alg_add_t_m2;
  1919.           cost_limit = cost;
  1920.         }
  1921.     }
  1922.     }
  1923.  
  1924.   /* Look for factors of t of the form
  1925.      t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
  1926.      If we find such a factor, we can multiply by t using an algorithm that
  1927.      multiplies by q, shift the result by m and add/subtract it to itself.
  1928.  
  1929.      We search for large factors first and loop down, even if large factors
  1930.      are less probable than small; if we find a large factor we will find a
  1931.      good sequence quickly, and therefore be able to prune (by decreasing
  1932.      COST_LIMIT) the search.  */
  1933.  
  1934.   for (m = floor_log2 (t - 1); m >= 2; m--)
  1935.     {
  1936.       unsigned HOST_WIDE_INT d;
  1937.  
  1938.       d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
  1939.       if (t % d == 0 && t > d)
  1940.     {
  1941.       cost = MIN (shiftadd_cost[m], add_cost + shift_cost[m]);
  1942.       synth_mult (alg_in, t / d, cost_limit - cost);
  1943.  
  1944.       cost += alg_in->cost;
  1945.       if (cost < cost_limit)
  1946.         {
  1947.           struct algorithm *x;
  1948.           x = alg_in, alg_in = best_alg, best_alg = x;
  1949.           best_alg->log[best_alg->ops] = m;
  1950.           best_alg->op[best_alg->ops] = alg_add_factor;
  1951.           cost_limit = cost;
  1952.         }
  1953.       /* Other factors will have been taken care of in the recursion.  */
  1954.       break;
  1955.     }
  1956.  
  1957.       d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
  1958.       if (t % d == 0 && t > d)
  1959.     {
  1960.       cost = MIN (shiftsub_cost[m], add_cost + shift_cost[m]);
  1961.       synth_mult (alg_in, t / d, cost_limit - cost);
  1962.  
  1963.       cost += alg_in->cost;
  1964.       if (cost < cost_limit)
  1965.         {
  1966.           struct algorithm *x;
  1967.           x = alg_in, alg_in = best_alg, best_alg = x;
  1968.           best_alg->log[best_alg->ops] = m;
  1969.           best_alg->op[best_alg->ops] = alg_sub_factor;
  1970.           cost_limit = cost;
  1971.         }
  1972.       break;
  1973.     }
  1974.     }
  1975.  
  1976.   /* Try shift-and-add (load effective address) instructions,
  1977.      i.e. do a*3, a*5, a*9.  */
  1978.   if ((t & 1) != 0)
  1979.     {
  1980.       q = t - 1;
  1981.       q = q & -q;
  1982.       m = exact_log2 (q);
  1983.       if (m >= 0)
  1984.     {
  1985.       cost = shiftadd_cost[m];
  1986.       synth_mult (alg_in, (t - 1) >> m, cost_limit - cost);
  1987.  
  1988.       cost += alg_in->cost;
  1989.       if (cost < cost_limit)
  1990.         {
  1991.           struct algorithm *x;
  1992.           x = alg_in, alg_in = best_alg, best_alg = x;
  1993.           best_alg->log[best_alg->ops] = m;
  1994.           best_alg->op[best_alg->ops] = alg_add_t2_m;
  1995.           cost_limit = cost;
  1996.         }
  1997.     }
  1998.  
  1999.       q = t + 1;
  2000.       q = q & -q;
  2001.       m = exact_log2 (q);
  2002.       if (m >= 0)
  2003.     {
  2004.       cost = shiftsub_cost[m];
  2005.       synth_mult (alg_in, (t + 1) >> m, cost_limit - cost);
  2006.  
  2007.       cost += alg_in->cost;
  2008.       if (cost < cost_limit)
  2009.         {
  2010.           struct algorithm *x;
  2011.           x = alg_in, alg_in = best_alg, best_alg = x;
  2012.           best_alg->log[best_alg->ops] = m;
  2013.           best_alg->op[best_alg->ops] = alg_sub_t2_m;
  2014.           cost_limit = cost;
  2015.         }
  2016.     }
  2017.     }
  2018.  
  2019.   /* If cost_limit has not decreased since we stored it in alg_out->cost,
  2020.      we have not found any algorithm.  */
  2021.   if (cost_limit == alg_out->cost)
  2022.     return;
  2023.  
  2024.   /* If we are getting a too long sequence for `struct algorithm'
  2025.      to record, make this search fail.  */
  2026.   if (best_alg->ops == MAX_BITS_PER_WORD)
  2027.     return;
  2028.  
  2029.   /* Copy the algorithm from temporary space to the space at alg_out.
  2030.      We avoid using structure assignment because the majority of
  2031.      best_alg is normally undefined, and this is a critical function.  */
  2032.   alg_out->ops = best_alg->ops + 1;
  2033.   alg_out->cost = cost_limit;
  2034.   bcopy ((char *) best_alg->op, (char *) alg_out->op,
  2035.      alg_out->ops * sizeof *alg_out->op);
  2036.   bcopy ((char *) best_alg->log, (char *) alg_out->log,
  2037.      alg_out->ops * sizeof *alg_out->log);
  2038. }
  2039.  
  2040. /* Perform a multiplication and return an rtx for the result.
  2041.    MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  2042.    TARGET is a suggestion for where to store the result (an rtx).
  2043.  
  2044.    We check specially for a constant integer as OP1.
  2045.    If you want this check for OP0 as well, then before calling
  2046.    you should swap the two operands if OP0 would be constant.  */
  2047.  
  2048. rtx
  2049. expand_mult (mode, op0, op1, target, unsignedp)
  2050.      enum machine_mode mode;
  2051.      register rtx op0, op1, target;
  2052.      int unsignedp;
  2053. {
  2054.   rtx const_op1 = op1;
  2055.  
  2056.   /* If we are multiplying in DImode, it may still be a win
  2057.      to try to work with shifts and adds.  */
  2058.   if (GET_CODE (op1) == CONST_DOUBLE
  2059.       && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
  2060.       && HOST_BITS_PER_INT <= BITS_PER_WORD)
  2061.     {
  2062.       if ((CONST_DOUBLE_HIGH (op1) == 0 && CONST_DOUBLE_LOW (op1) >= 0)
  2063.       || (CONST_DOUBLE_HIGH (op1) == -1 && CONST_DOUBLE_LOW (op1) < 0))
  2064.     const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
  2065.     }
  2066.  
  2067.   /* We used to test optimize here, on the grounds that it's better to
  2068.      produce a smaller program when -O is not used.
  2069.      But this causes such a terrible slowdown sometimes
  2070.      that it seems better to use synth_mult always.  */
  2071.  
  2072.   if (GET_CODE (const_op1) == CONST_INT)
  2073.     {
  2074.       struct algorithm alg;
  2075.       struct algorithm alg2;
  2076.       HOST_WIDE_INT val = INTVAL (op1);
  2077.       HOST_WIDE_INT val_so_far;
  2078.       rtx insn;
  2079.       int mult_cost;
  2080.       enum {basic_variant, negate_variant, add_variant} variant = basic_variant;
  2081.  
  2082.       /* Try to do the computation three ways: multiply by the negative of OP1
  2083.      and then negate, do the multiplication directly, or do multiplication
  2084.      by OP1 - 1.  */
  2085.  
  2086.       mult_cost = rtx_cost (gen_rtx (MULT, mode, op0, op1), SET);
  2087.       mult_cost = MIN (12 * add_cost, mult_cost);
  2088.  
  2089.       synth_mult (&alg, val, mult_cost);
  2090.       synth_mult (&alg2, - val,
  2091.           (alg.cost < mult_cost ? alg.cost : mult_cost) - negate_cost);
  2092.       if (alg2.cost + negate_cost < alg.cost)
  2093.     alg = alg2, variant = negate_variant;
  2094.  
  2095.       /* This proves very useful for division-by-constant.  */
  2096.       synth_mult (&alg2, val - 1, (alg.cost < mult_cost ? alg.cost : mult_cost) - add_cost);
  2097.       if (alg2.cost + add_cost < alg.cost)
  2098.     alg = alg2, variant = add_variant;
  2099.  
  2100.       if (alg.cost < mult_cost)
  2101.     {
  2102.       /* We found something cheaper than a multiply insn.  */
  2103.       int opno;
  2104.       rtx accum, tem;
  2105.  
  2106.       op0 = protect_from_queue (op0, 0);
  2107.  
  2108.       /* Avoid referencing memory over and over.
  2109.          For speed, but also for correctness when mem is volatile.  */
  2110.       if (GET_CODE (op0) == MEM)
  2111.         op0 = force_reg (mode, op0);
  2112.  
  2113.       /* ACCUM starts out either as OP0 or as a zero, depending on
  2114.          the first operation.  */
  2115.  
  2116.       if (alg.op[0] == alg_zero)
  2117.         {
  2118.           accum = copy_to_mode_reg (mode, const0_rtx);
  2119.           val_so_far = 0;
  2120.         }
  2121.       else if (alg.op[0] == alg_m)
  2122.         {
  2123.           accum = copy_to_mode_reg (mode, op0);
  2124.           val_so_far = 1;
  2125.         }
  2126.       else
  2127.         abort ();
  2128.  
  2129.       for (opno = 1; opno < alg.ops; opno++)
  2130.         {
  2131.           int log = alg.log[opno];
  2132.           int preserve = preserve_subexpressions_p ();
  2133.           rtx shift_subtarget = preserve ? 0 : accum;
  2134.           rtx add_target = opno == alg.ops - 1 && target != 0 ? target : 0;
  2135.           rtx accum_target = preserve ? 0 : accum;
  2136.           
  2137.           switch (alg.op[opno])
  2138.         {
  2139.         case alg_shift:
  2140.           accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2141.                     build_int_2 (log, 0), NULL_RTX, 0);
  2142.           val_so_far <<= log;
  2143.           break;
  2144.  
  2145.         case alg_add_t_m2:
  2146.           tem = expand_shift (LSHIFT_EXPR, mode, op0,
  2147.                       build_int_2 (log, 0), NULL_RTX, 0);
  2148.           accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
  2149.                      add_target ? add_target : accum_target);
  2150.           val_so_far += (HOST_WIDE_INT) 1 << log;
  2151.           break;
  2152.  
  2153.         case alg_sub_t_m2:
  2154.           tem = expand_shift (LSHIFT_EXPR, mode, op0,
  2155.                       build_int_2 (log, 0), NULL_RTX, 0);
  2156.           accum = force_operand (gen_rtx (MINUS, mode, accum, tem),
  2157.                      add_target ? add_target : accum_target);
  2158.           val_so_far -= (HOST_WIDE_INT) 1 << log;
  2159.           break;
  2160.  
  2161.         case alg_add_t2_m:
  2162.           accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2163.                     build_int_2 (log, 0), shift_subtarget,
  2164.                     0);
  2165.           accum = force_operand (gen_rtx (PLUS, mode, accum, op0),
  2166.                      add_target ? add_target : accum_target);
  2167.           val_so_far = (val_so_far << log) + 1;
  2168.           break;
  2169.  
  2170.         case alg_sub_t2_m:
  2171.           accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2172.                     build_int_2 (log, 0), shift_subtarget,
  2173.                     0);
  2174.           accum = force_operand (gen_rtx (MINUS, mode, accum, op0),
  2175.                      add_target ? add_target : accum_target);
  2176.           val_so_far = (val_so_far << log) - 1;
  2177.           break;
  2178.  
  2179.         case alg_add_factor:
  2180.           tem = expand_shift (LSHIFT_EXPR, mode, accum,
  2181.                       build_int_2 (log, 0), NULL_RTX, 0);
  2182.           accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
  2183.                      add_target ? add_target : accum_target);
  2184.           val_so_far += val_so_far << log;
  2185.           break;
  2186.  
  2187.         case alg_sub_factor:
  2188.           tem = expand_shift (LSHIFT_EXPR, mode, accum,
  2189.                       build_int_2 (log, 0), NULL_RTX, 0);
  2190.           accum = force_operand (gen_rtx (MINUS, mode, tem, accum),
  2191.                      (add_target ? add_target
  2192.                       : preserve ? 0 : tem));
  2193.           val_so_far = (val_so_far << log) - val_so_far;
  2194.           break;
  2195.  
  2196.         default:
  2197.           abort ();;
  2198.         }
  2199.  
  2200.           /* Write a REG_EQUAL note on the last insn so that we can cse
  2201.          multiplication sequences.  */
  2202.  
  2203.           insn = get_last_insn ();
  2204.           REG_NOTES (insn)
  2205.         = gen_rtx (EXPR_LIST, REG_EQUAL,
  2206.                gen_rtx (MULT, mode, op0, GEN_INT (val_so_far)),
  2207.                REG_NOTES (insn));
  2208.         }
  2209.  
  2210.       if (variant == negate_variant)
  2211.         {
  2212.           val_so_far = - val_so_far;
  2213.           accum = expand_unop (mode, neg_optab, accum, target, 0);
  2214.         }
  2215.       else if (variant == add_variant)
  2216.         {
  2217.           val_so_far = val_so_far + 1;
  2218.           accum = force_operand (gen_rtx (PLUS, mode, accum, op0), target);
  2219.         }
  2220.  
  2221.       if (val != val_so_far)
  2222.         abort ();
  2223.  
  2224.       return accum;
  2225.     }
  2226.     }
  2227.  
  2228.   /* This used to use umul_optab if unsigned, but for non-widening multiply
  2229.      there is no difference between signed and unsigned.  */
  2230.   op0 = expand_binop (mode, smul_optab,
  2231.               op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  2232.   if (op0 == 0)
  2233.     abort ();
  2234.   return op0;
  2235. }
  2236.  
  2237. /* Return the smallest n such that 2**n >= X.  */
  2238.  
  2239. int
  2240. ceil_log2 (x)
  2241.      unsigned HOST_WIDE_INT x;
  2242. {
  2243.   return floor_log2 (x - 1) + 1;
  2244. }
  2245.  
  2246. /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
  2247.    replace division by D, and put the least significant N bits of the result
  2248.    in *MULTIPLIER_PTR and return the most significant bit.
  2249.  
  2250.    The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
  2251.    needed precision is in PRECISION (should be <= N).
  2252.  
  2253.    PRECISION should be as small as possible so this function can choose
  2254.    multiplier more freely.
  2255.  
  2256.    The rounded-up logarithm of D is placed in *lgup_ptr.  A shift count that
  2257.    is to be used for a final right shift is placed in *POST_SHIFT_PTR.
  2258.  
  2259.    Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
  2260.    where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier.  */
  2261.  
  2262. static
  2263. unsigned HOST_WIDE_INT
  2264. choose_multiplier (d, n, precision, multiplier_ptr, post_shift_ptr, lgup_ptr)
  2265.      unsigned HOST_WIDE_INT d;
  2266.      int n;
  2267.      int precision;
  2268.      unsigned HOST_WIDE_INT *multiplier_ptr;
  2269.      int *post_shift_ptr;
  2270.      int *lgup_ptr;
  2271. {
  2272.   unsigned HOST_WIDE_INT mhigh_hi, mhigh_lo;
  2273.   unsigned HOST_WIDE_INT mlow_hi, mlow_lo;
  2274.   int lgup, post_shift;
  2275.   int pow, pow2;
  2276.   unsigned HOST_WIDE_INT nh, nl, dummy1, dummy2;
  2277.  
  2278.   /* lgup = ceil(log2(divisor)); */
  2279.   lgup = ceil_log2 (d);
  2280.  
  2281.   if (lgup > n)
  2282.     abort ();
  2283.  
  2284.   pow = n + lgup;
  2285.   pow2 = n + lgup - precision;
  2286.  
  2287.   if (pow == 2 * HOST_BITS_PER_WIDE_INT)
  2288.     {
  2289.       /* We could handle this with some effort, but this case is much better
  2290.      handled directly with a scc insn, so rely on caller using that.  */
  2291.       abort ();
  2292.     }
  2293.  
  2294.   /* mlow = 2^(N + lgup)/d */
  2295.  if (pow >= HOST_BITS_PER_WIDE_INT)
  2296.     {
  2297.       nh = (unsigned HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
  2298.       nl = 0;
  2299.     }
  2300.   else
  2301.     {
  2302.       nh = 0;
  2303.       nl = (unsigned HOST_WIDE_INT) 1 << pow;
  2304.     }
  2305.   div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
  2306.             &mlow_lo, &mlow_hi, &dummy1, &dummy2);
  2307.  
  2308.   /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
  2309.   if (pow2 >= HOST_BITS_PER_WIDE_INT)
  2310.     nh |= (unsigned HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
  2311.   else
  2312.     nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
  2313.   div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
  2314.             &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
  2315.  
  2316.   if (mhigh_hi && nh - d >= d)
  2317.     abort ();
  2318.   if (mhigh_hi > 1 || mlow_hi > 1)
  2319.     abort ();
  2320.   /* assert that mlow < mhigh.  */
  2321.   if (! (mlow_hi < mhigh_hi || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo)))
  2322.     abort();
  2323.  
  2324.   /* If precision == N, then mlow, mhigh exceed 2^N
  2325.      (but they do not exceed 2^(N+1)).  */
  2326.  
  2327.   /* Reduce to lowest terms */
  2328.   for (post_shift = lgup; post_shift > 0; post_shift--)
  2329.     {
  2330.       unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
  2331.       unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
  2332.       if (ml_lo >= mh_lo)
  2333.     break;
  2334.  
  2335.       mlow_hi = 0;
  2336.       mlow_lo = ml_lo;
  2337.       mhigh_hi = 0;
  2338.       mhigh_lo = mh_lo;
  2339.     }
  2340.  
  2341.   *post_shift_ptr = post_shift;
  2342.   *lgup_ptr = lgup;
  2343.   if (n < HOST_BITS_PER_WIDE_INT)
  2344.     {
  2345.       unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
  2346.       *multiplier_ptr = mhigh_lo & mask;
  2347.       return mhigh_lo >= mask;
  2348.     }
  2349.   else
  2350.     {
  2351.       *multiplier_ptr = mhigh_lo;
  2352.       return mhigh_hi;
  2353.     }
  2354. }
  2355.  
  2356. /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
  2357.    congruent to 1 (mod 2**N).  */
  2358.  
  2359. static unsigned HOST_WIDE_INT
  2360. invert_mod2n (x, n)
  2361.      unsigned HOST_WIDE_INT x;
  2362.      int n;
  2363. {
  2364.   /* Solve x*y == 1 (mod 2^n), where x is odd.  Return y. */
  2365.  
  2366.   /* The algorithm notes that the choice y = x satisfies
  2367.      x*y == 1 mod 2^3, since x is assumed odd.
  2368.      Each iteration doubles the number of bits of significance in y.  */
  2369.  
  2370.   unsigned HOST_WIDE_INT mask;
  2371.   unsigned HOST_WIDE_INT y = x;
  2372.   int nbit = 3;
  2373.  
  2374.   mask = (n == HOST_BITS_PER_WIDE_INT
  2375.       ? ~(unsigned HOST_WIDE_INT) 0
  2376.       : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
  2377.  
  2378.   while (nbit < n)
  2379.     {
  2380.       y = y * (2 - x*y) & mask;        /* Modulo 2^N */
  2381.       nbit *= 2;
  2382.     }
  2383.   return y;
  2384. }
  2385.  
  2386. /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
  2387.    flavor of OP0 and OP1.  ADJ_OPERAND is already the high half of the
  2388.    product OP0 x OP1.  If UNSIGNEDP is nonzero, adjust the signed product
  2389.    to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
  2390.    become signed.
  2391.  
  2392.    The result is put in TARGET if that is convenient.
  2393.  
  2394.    MODE is the mode of operation.  */
  2395.  
  2396. rtx
  2397. expand_mult_highpart_adjust (mode, adj_operand, op0, op1, target, unsignedp)
  2398.      enum machine_mode mode;
  2399.      register rtx adj_operand, op0, op1, target;
  2400.      int unsignedp;
  2401. {
  2402.   rtx tem;
  2403.   enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
  2404.  
  2405.   tem = expand_shift (RSHIFT_EXPR, mode, op0,
  2406.               build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
  2407.               NULL_RTX, 0);
  2408.   tem = expand_and (tem, op1, NULL_RTX);
  2409.   adj_operand = force_operand (gen_rtx (adj_code, mode, adj_operand, tem),
  2410.                    adj_operand);
  2411.  
  2412.   tem = expand_shift (RSHIFT_EXPR, mode, op1,
  2413.               build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
  2414.               NULL_RTX, 0);
  2415.   tem = expand_and (tem, op0, NULL_RTX);
  2416.   target = force_operand (gen_rtx (adj_code, mode, adj_operand, tem), target);
  2417.  
  2418.   return target;
  2419. }
  2420.  
  2421. /* Emit code to multiply OP0 and CNST1, putting the high half of the result
  2422.    in TARGET if that is convenient, and return where the result is.  If the
  2423.    operation can not be performed, 0 is returned.
  2424.  
  2425.    MODE is the mode of operation and result.
  2426.  
  2427.    UNSIGNEDP nonzero means unsigned multiply.  */
  2428.  
  2429. rtx
  2430. expand_mult_highpart (mode, op0, cnst1, target, unsignedp)
  2431.      enum machine_mode mode;
  2432.      register rtx op0, target;
  2433.      unsigned HOST_WIDE_INT cnst1;
  2434.      int unsignedp;
  2435. {
  2436.   enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
  2437.   optab mul_highpart_optab;
  2438.   optab moptab;
  2439.   rtx tem;
  2440.   int size = GET_MODE_BITSIZE (mode);
  2441.   rtx op1, wide_op1;
  2442.  
  2443.   /* We can't support modes wider than HOST_BITS_PER_INT.  */
  2444.   if (size > HOST_BITS_PER_WIDE_INT)
  2445.     abort ();
  2446.  
  2447.   op1 = GEN_INT (cnst1);
  2448.  
  2449.   if (GET_MODE_BITSIZE (wider_mode) <= HOST_BITS_PER_INT)
  2450.     wide_op1 = op1;
  2451.   else
  2452.     wide_op1
  2453.       = immed_double_const (cnst1,
  2454.                 (unsignedp
  2455.                  ? (HOST_WIDE_INT) 0
  2456.                  : -(cnst1 >> (HOST_BITS_PER_WIDE_INT - 1))),
  2457.                 wider_mode);
  2458.  
  2459.   /* expand_mult handles constant multiplication of word_mode
  2460.      or narrower.  It does a poor job for large modes.  */
  2461.   if (size < BITS_PER_WORD)
  2462.     {
  2463.       /* We have to do this, since expand_binop doesn't do conversion for
  2464.      multiply.  Maybe change expand_binop to handle widening multiply?  */
  2465.       op0 = convert_to_mode (wider_mode, op0, unsignedp);
  2466.  
  2467.       tem = expand_mult (wider_mode, op0, wide_op1, NULL_RTX, unsignedp);
  2468.       tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
  2469.               build_int_2 (size, 0), NULL_RTX, 1);
  2470.       return gen_lowpart (mode, tem);
  2471.     }
  2472.  
  2473.   if (target == 0)
  2474.     target = gen_reg_rtx (mode);
  2475.  
  2476.   /* Firstly, try using a multiplication insn that only generates the needed
  2477.      high part of the product, and in the sign flavor of unsignedp.  */
  2478.   mul_highpart_optab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
  2479.   target = expand_binop (mode, mul_highpart_optab,
  2480.              op0, op1, target, unsignedp, OPTAB_DIRECT);
  2481.   if (target)
  2482.     return target;
  2483.  
  2484.   /* Secondly, same as above, but use sign flavor opposite of unsignedp.
  2485.      Need to adjust the result after the multiplication.  */
  2486.   mul_highpart_optab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
  2487.   target = expand_binop (mode, mul_highpart_optab,
  2488.              op0, op1, target, unsignedp, OPTAB_DIRECT);
  2489.   if (target)
  2490.     /* We used the wrong signedness.  Adjust the result.  */
  2491.     return expand_mult_highpart_adjust (mode, target, op0,
  2492.                     op1, target, unsignedp);
  2493.  
  2494.   /* Thirdly, we try to use a widening multiplication, or a wider mode
  2495.      multiplication.  */
  2496.  
  2497.   moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
  2498.   if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing)
  2499.     ;
  2500.   else if (smul_optab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing)
  2501.     moptab = smul_optab;
  2502.   else
  2503.     {
  2504.       /* Try widening multiplication of opposite signedness, and adjust.  */
  2505.       moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
  2506.       if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing)
  2507.     {
  2508.       tem = expand_binop (wider_mode, moptab, op0, wide_op1,
  2509.                   NULL_RTX, ! unsignedp, OPTAB_WIDEN);
  2510.       if (tem != 0)
  2511.         {
  2512.           /* Extract the high half of the just generated product.  */
  2513.           tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
  2514.                   build_int_2 (size, 0), NULL_RTX, 1);
  2515.           tem = gen_lowpart (mode, tem);
  2516.           /* We used the wrong signedness.  Adjust the result.  */
  2517.           return expand_mult_highpart_adjust (mode, tem, op0, op1,
  2518.                           target, unsignedp);
  2519.         }
  2520.     }
  2521.  
  2522.       /* As a last resort, try widening the mode and perform a
  2523.      non-widening multiplication.  */
  2524.       moptab = smul_optab;
  2525.     }
  2526.  
  2527.   /* Pass NULL_RTX as target since TARGET has wrong mode.  */
  2528.   tem = expand_binop (wider_mode, moptab, op0, wide_op1,
  2529.               NULL_RTX, unsignedp, OPTAB_WIDEN);
  2530.   if (tem == 0)
  2531.     return 0;
  2532.  
  2533.   /* Extract the high half of the just generated product.  */
  2534.   tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
  2535.               build_int_2 (size, 0), NULL_RTX, 1);
  2536.   return gen_lowpart (mode, tem);
  2537. }
  2538.  
  2539. /* Emit the code to divide OP0 by OP1, putting the result in TARGET
  2540.    if that is convenient, and returning where the result is.
  2541.    You may request either the quotient or the remainder as the result;
  2542.    specify REM_FLAG nonzero to get the remainder.
  2543.  
  2544.    CODE is the expression code for which kind of division this is;
  2545.    it controls how rounding is done.  MODE is the machine mode to use.
  2546.    UNSIGNEDP nonzero means do unsigned division.  */
  2547.  
  2548. /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
  2549.    and then correct it by or'ing in missing high bits
  2550.    if result of ANDI is nonzero.
  2551.    For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
  2552.    This could optimize to a bfexts instruction.
  2553.    But C doesn't use these operations, so their optimizations are
  2554.    left for later.  */
  2555.  
  2556. #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
  2557.  
  2558. rtx
  2559. expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
  2560.      int rem_flag;
  2561.      enum tree_code code;
  2562.      enum machine_mode mode;
  2563.      register rtx op0, op1, target;
  2564.      int unsignedp;
  2565. {
  2566.   enum machine_mode compute_mode;
  2567.   register rtx tquotient;
  2568.   rtx quotient = 0, remainder = 0;
  2569.   rtx last;
  2570.   int size;
  2571.   rtx insn;
  2572.   optab optab1, optab2;
  2573.   int op1_is_constant, op1_is_pow2;
  2574.  
  2575.   op1_is_constant = GET_CODE (op1) == CONST_INT;
  2576.   op1_is_pow2 = (op1_is_constant
  2577.          && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
  2578.               || EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))));
  2579.  
  2580.   /*
  2581.      This is the structure of expand_divmod:
  2582.  
  2583.      First comes code to fix up the operands so we can perform the operations
  2584.      correctly and efficiently.
  2585.  
  2586.      Second comes a switch statement with code specific for each rounding mode.
  2587.      For some special operands this code emits all RTL for the desired
  2588.      operation, for other cases, it generates a quotient and stores it in
  2589.      QUOTIENT.  The case for trunc division/remainder might leave quotient = 0,
  2590.      to indicate that it has not done anything.
  2591.  
  2592.      Last comes code that finishes the operation.  If QUOTIENT is set an
  2593.      REM_FLAG, the remainder is computed as OP0 - QUOTIENT * OP1.  If QUOTIENT
  2594.      is not set, it is computed using trunc rounding.
  2595.  
  2596.      We try to generate special code for division and remainder when OP1 is a
  2597.      constant.  If |OP1| = 2**n we can use shifts and some other fast
  2598.      operations.  For other values of OP1, we compute a carefully selected
  2599.      fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
  2600.      by m.
  2601.  
  2602.      In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
  2603.      half of the product.  Different strategies for generating the product are
  2604.      implemented in expand_mult_highpart.
  2605.  
  2606.      If what we actually want is the remainder, we generate that by another
  2607.      by-constant multiplication and a subtraction.  */
  2608.  
  2609.   /* We shouldn't be called with OP1 == const1_rtx, but some of the
  2610.      code below will malfunction if we are, so check here and handle
  2611.      the special case if so.  */
  2612.   if (op1 == const1_rtx)
  2613.     return rem_flag ? const0_rtx : op0;
  2614.  
  2615.   if (target
  2616.       /* Don't use the function value register as a target
  2617.      since we have to read it as well as write it,
  2618.      and function-inlining gets confused by this.  */
  2619.       && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
  2620.       /* Don't clobber an operand while doing a multi-step calculation.  */
  2621.       || ((rem_flag || op1_is_constant)
  2622.           && (reg_mentioned_p (target, op0)
  2623.           || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
  2624.       || reg_mentioned_p (target, op1)
  2625.       || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM)))
  2626.     target = 0;
  2627.  
  2628.   /* Get the mode in which to perform this computation.  Normally it will
  2629.      be MODE, but sometimes we can't do the desired operation in MODE.
  2630.      If so, pick a wider mode in which we can do the operation.  Convert
  2631.      to that mode at the start to avoid repeated conversions.
  2632.  
  2633.      First see what operations we need.  These depend on the expression
  2634.      we are evaluating.  (We assume that divxx3 insns exist under the
  2635.      same conditions that modxx3 insns and that these insns don't normally
  2636.      fail.  If these assumptions are not correct, we may generate less
  2637.      efficient code in some cases.)
  2638.  
  2639.      Then see if we find a mode in which we can open-code that operation
  2640.      (either a division, modulus, or shift).  Finally, check for the smallest
  2641.      mode for which we can do the operation with a library call.  */
  2642.  
  2643.   /* We might want to refine this now that we have division-by-constant
  2644.      optimization.  Since expand_mult_highpart tries so many variants, it is
  2645.      not straightforward to generalize this.  Maybe we should make an array
  2646.      of possible modes in init_expmed?  Save this for GCC 2.7.  */
  2647.  
  2648.   optab1 = (op1_is_pow2 ? (unsignedp ? lshr_optab : ashr_optab)
  2649.         : (unsignedp ? udiv_optab : sdiv_optab));
  2650.   optab2 = (op1_is_pow2 ? optab1 : (unsignedp ? udivmod_optab : sdivmod_optab));
  2651.  
  2652.   for (compute_mode = mode; compute_mode != VOIDmode;
  2653.        compute_mode = GET_MODE_WIDER_MODE (compute_mode))
  2654.     if (optab1->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing
  2655.     || optab2->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing)
  2656.       break;
  2657.  
  2658.   if (compute_mode == VOIDmode)
  2659.     for (compute_mode = mode; compute_mode != VOIDmode;
  2660.      compute_mode = GET_MODE_WIDER_MODE (compute_mode))
  2661.       if (optab1->handlers[(int) compute_mode].libfunc
  2662.       || optab2->handlers[(int) compute_mode].libfunc)
  2663.     break;
  2664.  
  2665.   /* If we still couldn't find a mode, use MODE, but we'll probably abort
  2666.      in expand_binop.  */
  2667.   if (compute_mode == VOIDmode)
  2668.     compute_mode = mode;
  2669.  
  2670.   if (target && GET_MODE (target) == compute_mode)
  2671.     tquotient = target;
  2672.   else
  2673.     tquotient = gen_reg_rtx (compute_mode);
  2674.  
  2675.   size = GET_MODE_BITSIZE (compute_mode);
  2676. #if 0
  2677.   /* It should be possible to restrict the precision to GET_MODE_BITSIZE
  2678.      (mode), and thereby get better code when OP1 is a constant.  Do that for
  2679.      GCC 2.7.  It will require going over all usages of SIZE below.  */
  2680.   size = GET_MODE_BITSIZE (mode);
  2681. #endif
  2682.  
  2683.   /* Now convert to the best mode to use.  */
  2684.   if (compute_mode != mode)
  2685.     {
  2686.       op0 = convert_modes (compute_mode, mode, op0, unsignedp);
  2687.       op1 = convert_modes (compute_mode, mode, op1, unsignedp);
  2688.     }
  2689.  
  2690.   /* If one of the operands is a volatile MEM, copy it into a register.  */
  2691.  
  2692.   if (GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0))
  2693.     op0 = force_reg (compute_mode, op0);
  2694.   if (GET_CODE (op1) == MEM && MEM_VOLATILE_P (op1))
  2695.     op1 = force_reg (compute_mode, op1);
  2696.  
  2697.   /* If we need the remainder or if OP1 is constant, we need to
  2698.      put OP0 in a register in case it has any queued subexpressions.  */
  2699.   if (rem_flag || op1_is_constant)
  2700.     op0 = force_reg (compute_mode, op0);
  2701.  
  2702.   last = get_last_insn ();
  2703.  
  2704.   /* Promote floor rouding to trunc rounding for unsigned operations.  */
  2705.   if (unsignedp)
  2706.     {
  2707.       if (code == FLOOR_DIV_EXPR)
  2708.     code = TRUNC_DIV_EXPR;
  2709.       if (code == FLOOR_MOD_EXPR)
  2710.     code = TRUNC_MOD_EXPR;
  2711.     }
  2712.  
  2713.   if (op1 != const0_rtx)
  2714.     switch (code)
  2715.       {
  2716.       case TRUNC_MOD_EXPR:
  2717.       case TRUNC_DIV_EXPR:
  2718.     if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
  2719.       {
  2720.         if (unsignedp)
  2721.           {
  2722.         unsigned HOST_WIDE_INT mh, ml;
  2723.         int pre_shift, post_shift;
  2724.         int dummy;
  2725.         unsigned HOST_WIDE_INT d = INTVAL (op1);
  2726.  
  2727.         if (EXACT_POWER_OF_2_OR_ZERO_P (d))
  2728.           {
  2729.             pre_shift = floor_log2 (d);
  2730.             if (rem_flag)
  2731.               {
  2732.             remainder = expand_binop (compute_mode, and_optab, op0,
  2733.                           GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
  2734.                           remainder, 1,
  2735.                           OPTAB_LIB_WIDEN);
  2736.             if (remainder)
  2737.               return gen_lowpart (mode, remainder);
  2738.               }
  2739.             quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  2740.                          build_int_2 (pre_shift, 0),
  2741.                          tquotient, 1);
  2742.           }
  2743.         else if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
  2744.           {
  2745.             /* Most significant bit of divisor is set, emit a scc insn.
  2746.                emit_store_flag needs to be passed a place for the
  2747.                result.  */
  2748.             quotient = emit_store_flag (tquotient, GEU, op0, op1,
  2749.                         compute_mode, 1, 1);
  2750.             /* Can emit_store_flag have failed? */
  2751.             if (quotient == 0)
  2752.               goto fail1;
  2753.           }
  2754.         else
  2755.           {
  2756.             /* Find a suitable multiplier and right shift count instead
  2757.                of multiplying with D.  */
  2758.  
  2759.             mh = choose_multiplier (d, size, size,
  2760.                         &ml, &post_shift, &dummy);
  2761.  
  2762.             /* If the suggested multiplier is more than SIZE bits, we
  2763.                can do better for even divisors, using an initial right
  2764.                shift.  */
  2765.             if (mh != 0 && (d & 1) == 0)
  2766.               {
  2767.             pre_shift = floor_log2 (d & -d);
  2768.             mh = choose_multiplier (d >> pre_shift, size,
  2769.                         size - pre_shift,
  2770.                         &ml, &post_shift, &dummy);
  2771.             if (mh)
  2772.               abort ();
  2773.               }
  2774.             else
  2775.               pre_shift = 0;
  2776.  
  2777.             if (mh != 0)
  2778.               {
  2779.             rtx t1, t2, t3, t4;
  2780.  
  2781.             t1 = expand_mult_highpart (compute_mode, op0, ml,
  2782.                            NULL_RTX, 1);
  2783.             if (t1 == 0)
  2784.               goto fail1;
  2785.             t2 = force_operand (gen_rtx (MINUS, compute_mode,
  2786.                              op0, t1),
  2787.                         NULL_RTX);
  2788.             t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
  2789.                        build_int_2 (1, 0), NULL_RTX, 1);
  2790.             t4 = force_operand (gen_rtx (PLUS, compute_mode,
  2791.                              t1, t3),
  2792.                         NULL_RTX);
  2793.             quotient = expand_shift (RSHIFT_EXPR, compute_mode, t4,
  2794.                          build_int_2 (post_shift - 1,
  2795.                                   0),
  2796.                          tquotient, 1);
  2797.               }
  2798.             else
  2799.               {
  2800.             rtx t1, t2;
  2801.  
  2802.             t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  2803.                        build_int_2 (pre_shift, 0),
  2804.                        NULL_RTX, 1);
  2805.             t2 = expand_mult_highpart (compute_mode, t1, ml,
  2806.                            NULL_RTX, 1);
  2807.             if (t2 == 0)
  2808.               goto fail1;
  2809.             quotient = expand_shift (RSHIFT_EXPR, compute_mode, t2,
  2810.                          build_int_2 (post_shift, 0),
  2811.                          tquotient, 1);
  2812.               }
  2813.           }
  2814.  
  2815.         insn = get_last_insn ();
  2816.         REG_NOTES (insn)
  2817.           = gen_rtx (EXPR_LIST, REG_EQUAL,
  2818.                  gen_rtx (UDIV, compute_mode, op0, op1),
  2819.                  REG_NOTES (insn));
  2820.           }
  2821.         else        /* TRUNC_DIV, signed */
  2822.           {
  2823.         unsigned HOST_WIDE_INT ml;
  2824.         int lgup, post_shift;
  2825.         HOST_WIDE_INT d = INTVAL (op1);
  2826.         unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
  2827.  
  2828.         /* n rem d = n rem -d */
  2829.         if (rem_flag && d < 0)
  2830.           {
  2831.             d = abs_d;
  2832.             op1 = GEN_INT (abs_d);
  2833.           }
  2834.  
  2835.         if (d == 1)
  2836.           quotient = op0;
  2837.         else if (d == -1)
  2838.           quotient = expand_unop (compute_mode, neg_optab, op0,
  2839.                       tquotient, 0);
  2840.         else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
  2841.              && (rem_flag ? smod_pow2_cheap : sdiv_pow2_cheap))
  2842.           ;
  2843.         else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
  2844.           {
  2845.             lgup = floor_log2 (abs_d);
  2846.             if (abs_d != 2 && BRANCH_COST < 3)
  2847.               {
  2848.             rtx label = gen_label_rtx ();
  2849.             rtx t1;
  2850.  
  2851.             t1 = copy_to_mode_reg (compute_mode, op0);
  2852.             emit_cmp_insn (t1, const0_rtx, GE, 
  2853.                        NULL_RTX, compute_mode, 0, 0);
  2854.             emit_jump_insn (gen_bge (label));
  2855.             expand_inc (t1, GEN_INT (abs_d - 1));
  2856.             emit_label (label);
  2857.             quotient = expand_shift (RSHIFT_EXPR, compute_mode, t1,
  2858.                          build_int_2 (lgup, 0),
  2859.                          tquotient, 0);
  2860.               }
  2861.             else
  2862.               {
  2863.             rtx t1, t2, t3;
  2864.             t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  2865.                        build_int_2 (size - 1, 0),
  2866.                        NULL_RTX, 0);
  2867.             t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
  2868.                        build_int_2 (size - lgup, 0),
  2869.                        NULL_RTX, 1);
  2870.             t3 = force_operand (gen_rtx (PLUS, compute_mode,
  2871.                              op0, t2),
  2872.                         NULL_RTX);
  2873.             quotient = expand_shift (RSHIFT_EXPR, compute_mode, t3,
  2874.                          build_int_2 (lgup, 0),
  2875.                          tquotient, 0);
  2876.               }
  2877.  
  2878.             if (d < 0)
  2879.               {
  2880.             insn = get_last_insn ();
  2881.             REG_NOTES (insn)
  2882.               = gen_rtx (EXPR_LIST, REG_EQUAL,
  2883.                      gen_rtx (DIV, compute_mode, op0,
  2884.                           GEN_INT (abs_d)),
  2885.                      REG_NOTES (insn));
  2886.  
  2887.             quotient = expand_unop (compute_mode, neg_optab,
  2888.                         quotient, quotient, 0);
  2889.               }
  2890.           }
  2891.         else
  2892.           {
  2893.             choose_multiplier (abs_d, size, size - 1,
  2894.                        &ml, &post_shift, &lgup);
  2895.             if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
  2896.               {
  2897.             rtx t1, t2, t3;
  2898.  
  2899.             t1 = expand_mult_highpart (compute_mode, op0, ml,
  2900.                            NULL_RTX, 0);
  2901.             if (t1 == 0)
  2902.               goto fail1;
  2903.             t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
  2904.                        build_int_2 (post_shift, 0), NULL_RTX, 0);
  2905.             t3 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  2906.                        build_int_2 (size - 1, 0), NULL_RTX, 0);
  2907.             if (d < 0)
  2908.               quotient = force_operand (gen_rtx (MINUS, compute_mode, t3, t2),
  2909.                             tquotient);
  2910.             else
  2911.               quotient = force_operand (gen_rtx (MINUS, compute_mode, t2, t3),
  2912.                             tquotient);
  2913.               }
  2914.             else
  2915.               {
  2916.             rtx t1, t2, t3, t4;
  2917.  
  2918.             ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
  2919.             t1 = expand_mult_highpart (compute_mode, op0, ml,
  2920.                            NULL_RTX, 0);
  2921.             if (t1 == 0)
  2922.               goto fail1;
  2923.             t2 = force_operand (gen_rtx (PLUS, compute_mode, t1, op0),
  2924.                         NULL_RTX);
  2925.             t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
  2926.                        build_int_2 (post_shift, 0), NULL_RTX, 0);
  2927.             t4 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  2928.                        build_int_2 (size - 1, 0), NULL_RTX, 0);
  2929.             if (d < 0)
  2930.               quotient = force_operand (gen_rtx (MINUS, compute_mode, t4, t3),
  2931.                             tquotient);
  2932.             else
  2933.               quotient = force_operand (gen_rtx (MINUS, compute_mode, t3, t4),
  2934.                             tquotient);
  2935.               }
  2936.           }
  2937.  
  2938.         if (quotient != 0)
  2939.           {
  2940.             insn = get_last_insn ();
  2941.             REG_NOTES (insn)
  2942.               = gen_rtx (EXPR_LIST, REG_EQUAL,
  2943.                  gen_rtx (DIV, compute_mode, op0, op1),
  2944.                  REG_NOTES (insn));
  2945.           }
  2946.           }
  2947.         break;
  2948.       }
  2949.       fail1:
  2950.     delete_insns_since (last);
  2951.     break;
  2952.  
  2953.       case FLOOR_DIV_EXPR:
  2954.       case FLOOR_MOD_EXPR:
  2955.       /* We will come here only for signed operations.  */
  2956.     if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
  2957.       {
  2958.         unsigned HOST_WIDE_INT mh, ml;
  2959.         int pre_shift, lgup, post_shift;
  2960.         HOST_WIDE_INT d = INTVAL (op1);
  2961.  
  2962.         if (d > 0)
  2963.           {
  2964.         /* We could just as easily deal with negative constants here,
  2965.            but it does not seem worth the trouble for GCC 2.6.  */
  2966.         if (EXACT_POWER_OF_2_OR_ZERO_P (d))
  2967.           {
  2968.             pre_shift = floor_log2 (d);
  2969.             if (rem_flag)
  2970.               {
  2971.             remainder = expand_binop (compute_mode, and_optab, op0,
  2972.                           GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
  2973.                           remainder, 0, OPTAB_LIB_WIDEN);
  2974.             if (remainder)
  2975.               return gen_lowpart (mode, remainder);
  2976.               }
  2977.             quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  2978.                          build_int_2 (pre_shift, 0),
  2979.                          tquotient, 0);
  2980.           }
  2981.         else
  2982.           {
  2983.             rtx t1, t2, t3, t4;
  2984.  
  2985.             mh = choose_multiplier (d, size, size - 1,
  2986.                         &ml, &post_shift, &lgup);
  2987.             if (mh)
  2988.               abort ();
  2989.  
  2990.             t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  2991.                        build_int_2 (size - 1, 0), NULL_RTX, 0);
  2992.             t2 = expand_binop (compute_mode, xor_optab, op0, t1,
  2993.                        NULL_RTX, 0, OPTAB_WIDEN);
  2994.             t3 = expand_mult_highpart (compute_mode, t2, ml,
  2995.                            NULL_RTX, 1);
  2996.             if (t3 != 0)
  2997.               {
  2998.             t4 = expand_shift (RSHIFT_EXPR, compute_mode, t3,
  2999.                        build_int_2 (post_shift, 0),
  3000.                        NULL_RTX, 1);
  3001.             quotient = expand_binop (compute_mode, xor_optab,
  3002.                          t4, t1, tquotient, 0,
  3003.                          OPTAB_WIDEN);
  3004.               }
  3005.           }
  3006.           }
  3007.         else
  3008.           {
  3009.         rtx nsign, t1, t2, t3, t4;
  3010.         t1 = force_operand (gen_rtx (PLUS, compute_mode,
  3011.                          op0, constm1_rtx), NULL_RTX);
  3012.         t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
  3013.                    0, OPTAB_WIDEN);
  3014.         nsign = expand_shift (RSHIFT_EXPR, compute_mode, t2,
  3015.                       build_int_2 (size - 1, 0), NULL_RTX, 0);
  3016.         t3 = force_operand (gen_rtx (MINUS, compute_mode, t1, nsign),
  3017.                     NULL_RTX);
  3018.         t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
  3019.                     NULL_RTX, 0);
  3020.         if (t4)
  3021.           {
  3022.             rtx t5;
  3023.             t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
  3024.                       NULL_RTX, 0);
  3025.             quotient = force_operand (gen_rtx (PLUS, compute_mode,
  3026.                                t4, t5),
  3027.                           tquotient);
  3028.           }
  3029.           }
  3030.       }
  3031.  
  3032.     if (quotient != 0)
  3033.       break;
  3034.     delete_insns_since (last);
  3035.  
  3036.     /* Try using an instruction that produces both the quotient and
  3037.        remainder, using truncation.  We can easily compensate the quotient
  3038.        or remainder to get floor rounding, once we have the remainder.
  3039.        Notice that we compute also the final remainder value here,
  3040.        and return the result right away.  */
  3041.     if (target == 0)
  3042.       target = gen_reg_rtx (compute_mode);
  3043.     if (rem_flag)
  3044.       {
  3045.         remainder = target;
  3046.         quotient = gen_reg_rtx (compute_mode);
  3047.       }
  3048.     else
  3049.       {
  3050.         quotient = target;
  3051.         remainder = gen_reg_rtx (compute_mode);
  3052.       }
  3053.  
  3054.     if (expand_twoval_binop (sdivmod_optab, op0, op1,
  3055.                  quotient, remainder, 0))
  3056.       {
  3057.         /* This could be computed with a branch-less sequence.
  3058.            Save that for later.  */
  3059.         rtx tem;
  3060.         rtx label = gen_label_rtx ();
  3061.         emit_cmp_insn (remainder, const0_rtx, EQ, NULL_RTX,
  3062.                compute_mode, 0, 0);
  3063.         emit_jump_insn (gen_beq (label));
  3064.         tem = expand_binop (compute_mode, xor_optab, op0, op1,
  3065.                 NULL_RTX, 0, OPTAB_WIDEN);
  3066.         emit_cmp_insn (tem, const0_rtx, GE, NULL_RTX, compute_mode, 0, 0);
  3067.         emit_jump_insn (gen_bge (label));
  3068.         expand_dec (quotient, const1_rtx);
  3069.         expand_inc (remainder, op1);
  3070.         emit_label (label);
  3071.         return gen_lowpart (mode, rem_flag ? remainder : quotient);
  3072.       }
  3073.  
  3074.     /* No luck with division elimination or divmod.  Have to do it
  3075.        by conditionally adjusting op0 *and* the result.  */
  3076.     {
  3077.       rtx label1, label2, label3, label4, label5;
  3078.       rtx adjusted_op0;
  3079.       rtx tem;
  3080.  
  3081.       quotient = gen_reg_rtx (compute_mode);
  3082.       adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
  3083.       label1 = gen_label_rtx ();
  3084.       label2 = gen_label_rtx ();
  3085.       label3 = gen_label_rtx ();
  3086.       label4 = gen_label_rtx ();
  3087.       label5 = gen_label_rtx ();
  3088.       emit_cmp_insn (op1, const0_rtx, LT, NULL_RTX, compute_mode, 0, 0);
  3089.       emit_jump_insn (gen_blt (label2));
  3090.       emit_cmp_insn (adjusted_op0, const0_rtx, LT, NULL_RTX,
  3091.              compute_mode, 0, 0);
  3092.       emit_jump_insn (gen_blt (label1));
  3093.       tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  3094.                   quotient, 0, OPTAB_LIB_WIDEN);
  3095.       if (tem != quotient)
  3096.         emit_move_insn (quotient, tem);
  3097.       emit_jump_insn (gen_jump (label5));
  3098.       emit_barrier ();
  3099.       emit_label (label1);
  3100.       expand_inc (adjusted_op0, const1_rtx);
  3101.       emit_jump_insn (gen_jump (label4));
  3102.       emit_barrier ();
  3103.       emit_label (label2);
  3104.       emit_cmp_insn (adjusted_op0, const0_rtx, GT, NULL_RTX,
  3105.              compute_mode, 0, 0);
  3106.       emit_jump_insn (gen_bgt (label3));
  3107.       tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  3108.                   quotient, 0, OPTAB_LIB_WIDEN);
  3109.       if (tem != quotient)
  3110.         emit_move_insn (quotient, tem);
  3111.       emit_jump_insn (gen_jump (label5));
  3112.       emit_barrier ();
  3113.       emit_label (label3);
  3114.       expand_dec (adjusted_op0, const1_rtx);
  3115.       emit_label (label4);
  3116.       tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  3117.                   quotient, 0, OPTAB_LIB_WIDEN);
  3118.       if (tem != quotient)
  3119.         emit_move_insn (quotient, tem);
  3120.       expand_dec (quotient, const1_rtx);
  3121.       emit_label (label5);
  3122.     }
  3123.     break;
  3124.  
  3125.       case CEIL_DIV_EXPR:
  3126.       case CEIL_MOD_EXPR:
  3127.     if (unsignedp)
  3128.       {
  3129.         if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
  3130.           {
  3131.         rtx t1, t2, t3;
  3132.         unsigned HOST_WIDE_INT d = INTVAL (op1);
  3133.         t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
  3134.                    build_int_2 (floor_log2 (d), 0),
  3135.                    tquotient, 1);
  3136.         t2 = expand_binop (compute_mode, and_optab, op0,
  3137.                    GEN_INT (d - 1),
  3138.                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
  3139.         t3 = gen_reg_rtx (compute_mode);
  3140.         t3 = emit_store_flag (t3, NE, t2, const0_rtx,
  3141.                       compute_mode, 1, 1);
  3142.         if (t3 == 0)
  3143.           {
  3144.             rtx lab;
  3145.             lab = gen_label_rtx ();
  3146.             emit_cmp_insn (t2, const0_rtx, EQ, NULL_RTX,
  3147.                    compute_mode, 0, 0);
  3148.             emit_jump_insn (gen_beq (lab));
  3149.             expand_inc (t1, const1_rtx);
  3150.             emit_label (lab);
  3151.             quotient = t1;
  3152.           }
  3153.         else
  3154.           quotient = force_operand (gen_rtx (PLUS, compute_mode,
  3155.                              t1, t3),
  3156.                         tquotient);
  3157.         break;
  3158.           }
  3159.  
  3160.         /* Try using an instruction that produces both the quotient and
  3161.            remainder, using truncation.  We can easily compensate the
  3162.            quotient or remainder to get ceiling rounding, once we have the
  3163.            remainder.  Notice that we compute also the final remainder
  3164.            value here, and return the result right away.  */
  3165.         if (target == 0)
  3166.           target = gen_reg_rtx (compute_mode);
  3167.         if (rem_flag)
  3168.           {
  3169.         remainder = target;
  3170.         quotient = gen_reg_rtx (compute_mode);
  3171.           }
  3172.         else
  3173.           {
  3174.         quotient = target;
  3175.         remainder = gen_reg_rtx (compute_mode);
  3176.           }
  3177.  
  3178.         if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
  3179.                      remainder, 1))
  3180.           {
  3181.         /* This could be computed with a branch-less sequence.
  3182.            Save that for later.  */
  3183.         rtx label = gen_label_rtx ();
  3184.         emit_cmp_insn (remainder, const0_rtx, EQ, NULL_RTX,
  3185.                    compute_mode, 0, 0);
  3186.         emit_jump_insn (gen_beq (label));
  3187.         expand_inc (quotient, const1_rtx);
  3188.         expand_dec (remainder, op1);
  3189.         emit_label (label);
  3190.         return gen_lowpart (mode, rem_flag ? remainder : quotient);
  3191.           }
  3192.  
  3193.         /* No luck with division elimination or divmod.  Have to do it
  3194.            by conditionally adjusting op0 *and* the result.  */
  3195.         {
  3196.           rtx label1, label2;
  3197.           rtx adjusted_op0, tem;
  3198.  
  3199.           quotient = gen_reg_rtx (compute_mode);
  3200.           adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
  3201.           label1 = gen_label_rtx ();
  3202.           label2 = gen_label_rtx ();
  3203.           emit_cmp_insn (adjusted_op0, const0_rtx, NE, NULL_RTX,
  3204.                  compute_mode, 0, 0);
  3205.           emit_jump_insn (gen_bne (label1));
  3206.           emit_move_insn  (quotient, const0_rtx);
  3207.           emit_jump_insn (gen_jump (label2));
  3208.           emit_barrier ();
  3209.           emit_label (label1);
  3210.           expand_dec (adjusted_op0, const1_rtx);
  3211.           tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
  3212.                   quotient, 1, OPTAB_LIB_WIDEN);
  3213.           if (tem != quotient)
  3214.         emit_move_insn (quotient, tem);
  3215.           expand_inc (quotient, const1_rtx);
  3216.           emit_label (label2);
  3217.         }
  3218.       }
  3219.     else /* signed */
  3220.       {
  3221.         /* Try using an instruction that produces both the quotient and
  3222.            remainder, using truncation.  We can easily compensate the
  3223.            quotient or remainder to get ceiling rounding, once we have the
  3224.            remainder.  Notice that we compute also the final remainder
  3225.            value here, and return the result right away.  */
  3226.         if (target == 0)
  3227.           target = gen_reg_rtx (compute_mode);
  3228.         if (rem_flag)
  3229.           {
  3230.         remainder = target;
  3231.         quotient = gen_reg_rtx (compute_mode);
  3232.           }
  3233.         else
  3234.           {
  3235.         quotient = target;
  3236.         remainder = gen_reg_rtx (compute_mode);
  3237.           }
  3238.  
  3239.         if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
  3240.                      remainder, 0))
  3241.           {
  3242.         /* This could be computed with a branch-less sequence.
  3243.            Save that for later.  */
  3244.         rtx tem;
  3245.         rtx label = gen_label_rtx ();
  3246.         emit_cmp_insn (remainder, const0_rtx, EQ, NULL_RTX,
  3247.                    compute_mode, 0, 0);
  3248.         emit_jump_insn (gen_beq (label));
  3249.         tem = expand_binop (compute_mode, xor_optab, op0, op1,
  3250.                     NULL_RTX, 0, OPTAB_WIDEN);
  3251.         emit_cmp_insn (tem, const0_rtx, LT, NULL_RTX,
  3252.                    compute_mode, 0, 0);
  3253.         emit_jump_insn (gen_blt (label));
  3254.         expand_inc (quotient, const1_rtx);
  3255.         expand_dec (remainder, op1);
  3256.         emit_label (label);
  3257.         return gen_lowpart (mode, rem_flag ? remainder : quotient);
  3258.           }
  3259.  
  3260.         /* No luck with division elimination or divmod.  Have to do it
  3261.            by conditionally adjusting op0 *and* the result.  */
  3262.         {
  3263.           rtx label1, label2, label3, label4, label5;
  3264.           rtx adjusted_op0;
  3265.           rtx tem;
  3266.  
  3267.           quotient = gen_reg_rtx (compute_mode);
  3268.           adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
  3269.           label1 = gen_label_rtx ();
  3270.           label2 = gen_label_rtx ();
  3271.           label3 = gen_label_rtx ();
  3272.           label4 = gen_label_rtx ();
  3273.           label5 = gen_label_rtx ();
  3274.           emit_cmp_insn (op1, const0_rtx, LT, NULL_RTX,
  3275.                  compute_mode, 0, 0);
  3276.           emit_jump_insn (gen_blt (label2));
  3277.           emit_cmp_insn (adjusted_op0, const0_rtx, GT, NULL_RTX,
  3278.                  compute_mode, 0, 0);
  3279.           emit_jump_insn (gen_bgt (label1));
  3280.           tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  3281.                   quotient, 0, OPTAB_LIB_WIDEN);
  3282.           if (tem != quotient)
  3283.         emit_move_insn (quotient, tem);
  3284.           emit_jump_insn (gen_jump (label5));
  3285.           emit_barrier ();
  3286.           emit_label (label1);
  3287.           expand_dec (adjusted_op0, const1_rtx);
  3288.           emit_jump_insn (gen_jump (label4));
  3289.           emit_barrier ();
  3290.           emit_label (label2);
  3291.           emit_cmp_insn (adjusted_op0, const0_rtx, LT, NULL_RTX,
  3292.                  compute_mode, 0, 0);
  3293.           emit_jump_insn (gen_blt (label3));
  3294.           tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  3295.                   quotient, 0, OPTAB_LIB_WIDEN);
  3296.           if (tem != quotient)
  3297.         emit_move_insn (quotient, tem);
  3298.           emit_jump_insn (gen_jump (label5));
  3299.           emit_barrier ();
  3300.           emit_label (label3);
  3301.           expand_inc (adjusted_op0, const1_rtx);
  3302.           emit_label (label4);
  3303.           tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
  3304.                   quotient, 0, OPTAB_LIB_WIDEN);
  3305.           if (tem != quotient)
  3306.         emit_move_insn (quotient, tem);
  3307.           expand_inc (quotient, const1_rtx);
  3308.           emit_label (label5);
  3309.         }
  3310.       }
  3311.     break;
  3312.  
  3313.       case EXACT_DIV_EXPR:
  3314.     if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
  3315.       {
  3316.         HOST_WIDE_INT d = INTVAL (op1);
  3317.         unsigned HOST_WIDE_INT ml;
  3318.         int post_shift;
  3319.         rtx t1;
  3320.  
  3321.         post_shift = floor_log2 (d & -d);
  3322.         ml = invert_mod2n (d >> post_shift, size);
  3323.         t1 = expand_mult (compute_mode, op0, GEN_INT (ml), NULL_RTX,
  3324.                   unsignedp);
  3325.         quotient = expand_shift (RSHIFT_EXPR, compute_mode, t1,
  3326.                      build_int_2 (post_shift, 0),
  3327.                      NULL_RTX, unsignedp);
  3328.  
  3329.         insn = get_last_insn ();
  3330.         REG_NOTES (insn)
  3331.           = gen_rtx (EXPR_LIST, REG_EQUAL,
  3332.              gen_rtx (unsignedp ? UDIV : DIV, compute_mode,
  3333.                   op0, op1),
  3334.              REG_NOTES (insn));
  3335.       }
  3336.     break;
  3337.  
  3338.       case ROUND_DIV_EXPR:
  3339.       case ROUND_MOD_EXPR:
  3340.     /* The code that used to be here was wrong, and nothing really
  3341.        depends on it.  */
  3342.     abort ();
  3343.     break;
  3344.       }
  3345.  
  3346.   if (quotient == 0)
  3347.     {
  3348.       if (rem_flag)
  3349.     {
  3350.       /* Try to produce the remainder directly without a library call.  */
  3351.       remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
  3352.                      op0, op1, target,
  3353.                      unsignedp, OPTAB_WIDEN);
  3354.       if (remainder == 0)
  3355.         {
  3356.           /* No luck there.  Can we do remainder and divide at once
  3357.          without a library call?  */
  3358.           remainder = gen_reg_rtx (compute_mode);
  3359.           if (! expand_twoval_binop ((unsignedp
  3360.                       ? udivmod_optab
  3361.                       : sdivmod_optab),
  3362.                      op0, op1,
  3363.                      NULL_RTX, remainder, unsignedp))
  3364.         remainder = 0;
  3365.         }
  3366.  
  3367.       if (remainder)
  3368.         return gen_lowpart (mode, remainder);
  3369.     }
  3370.  
  3371.       /* Produce the quotient.  */
  3372.       /* Try a quotient insn, but not a library call.  */
  3373.       quotient = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
  3374.                     op0, op1, rem_flag ? NULL_RTX : target,
  3375.                     unsignedp, OPTAB_WIDEN);
  3376.       if (quotient == 0)
  3377.     {
  3378.       /* No luck there.  Try a quotient-and-remainder insn,
  3379.          keeping the quotient alone.  */
  3380.       quotient = gen_reg_rtx (compute_mode);
  3381.       if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  3382.                      op0, op1,
  3383.                      quotient, NULL_RTX, unsignedp))
  3384.         {
  3385.           quotient = 0;
  3386.           if (! rem_flag)
  3387.         /* Still no luck.  If we are not computing the remainder,
  3388.            use a library call for the quotient.  */
  3389.         quotient = sign_expand_binop (compute_mode,
  3390.                           udiv_optab, sdiv_optab,
  3391.                           op0, op1, target,
  3392.                           unsignedp, OPTAB_LIB_WIDEN);
  3393.         }
  3394.     }
  3395.     }
  3396.  
  3397.   if (rem_flag)
  3398.     {
  3399.       if (quotient == 0)
  3400.     /* No divide instruction either.  Use library for remainder.  */
  3401.     remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
  3402.                        op0, op1, target,
  3403.                        unsignedp, OPTAB_LIB_WIDEN);
  3404.       else
  3405.     {
  3406.       /* We divided.  Now finish doing X - Y * (X / Y).  */
  3407.       remainder = expand_mult (compute_mode, quotient, op1,
  3408.                    NULL_RTX, unsignedp);
  3409.       remainder = expand_binop (compute_mode, sub_optab, op0,
  3410.                     remainder, target, unsignedp,
  3411.                     OPTAB_LIB_WIDEN);
  3412.     }
  3413.     }
  3414.  
  3415.   return gen_lowpart (mode, rem_flag ? remainder : quotient);
  3416. }
  3417.  
  3418. /* Return a tree node with data type TYPE, describing the value of X.
  3419.    Usually this is an RTL_EXPR, if there is no obvious better choice.
  3420.    X may be an expression, however we only support those expressions
  3421.    generated by loop.c.   */
  3422.  
  3423. tree
  3424. make_tree (type, x)
  3425.      tree type;
  3426.      rtx x;
  3427. {
  3428.   tree t;
  3429.  
  3430.   switch (GET_CODE (x))
  3431.     {
  3432.     case CONST_INT:
  3433.       t = build_int_2 (INTVAL (x),
  3434.                TREE_UNSIGNED (type) || INTVAL (x) >= 0 ? 0 : -1);
  3435.       TREE_TYPE (t) = type;
  3436.       return t;
  3437.  
  3438.     case CONST_DOUBLE:
  3439.       if (GET_MODE (x) == VOIDmode)
  3440.     {
  3441.       t = build_int_2 (CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
  3442.       TREE_TYPE (t) = type;
  3443.     }
  3444.       else
  3445.     {
  3446.       REAL_VALUE_TYPE d;
  3447.  
  3448.       REAL_VALUE_FROM_CONST_DOUBLE (d, x);
  3449.       t = build_real (type, d);
  3450.     }
  3451.  
  3452.       return t;
  3453.       
  3454.     case PLUS:
  3455.       return fold (build (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
  3456.               make_tree (type, XEXP (x, 1))));
  3457.                                
  3458.     case MINUS:
  3459.       return fold (build (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
  3460.               make_tree (type, XEXP (x, 1))));
  3461.                                
  3462.     case NEG:
  3463.       return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
  3464.  
  3465.     case MULT:
  3466.       return fold (build (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
  3467.               make_tree (type, XEXP (x, 1))));
  3468.                               
  3469.     case ASHIFT:
  3470.       return fold (build (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
  3471.               make_tree (type, XEXP (x, 1))));
  3472.                               
  3473.     case LSHIFTRT:
  3474.       return fold (convert (type,
  3475.                 build (RSHIFT_EXPR, unsigned_type (type),
  3476.                    make_tree (unsigned_type (type),
  3477.                           XEXP (x, 0)),
  3478.                    make_tree (type, XEXP (x, 1)))));
  3479.                               
  3480.     case ASHIFTRT:
  3481.       return fold (convert (type,
  3482.                 build (RSHIFT_EXPR, signed_type (type),
  3483.                    make_tree (signed_type (type), XEXP (x, 0)),
  3484.                    make_tree (type, XEXP (x, 1)))));
  3485.                               
  3486.     case DIV:
  3487.       if (TREE_CODE (type) != REAL_TYPE)
  3488.     t = signed_type (type);
  3489.       else
  3490.     t = type;
  3491.  
  3492.       return fold (convert (type,
  3493.                 build (TRUNC_DIV_EXPR, t,
  3494.                    make_tree (t, XEXP (x, 0)),
  3495.                    make_tree (t, XEXP (x, 1)))));
  3496.     case UDIV:
  3497.       t = unsigned_type (type);
  3498.       return fold (convert (type,
  3499.                 build (TRUNC_DIV_EXPR, t,
  3500.                    make_tree (t, XEXP (x, 0)),
  3501.                    make_tree (t, XEXP (x, 1)))));
  3502.    default:
  3503.       t = make_node (RTL_EXPR);
  3504.       TREE_TYPE (t) = type;
  3505.       RTL_EXPR_RTL (t) = x;
  3506.       /* There are no insns to be output
  3507.      when this rtl_expr is used.  */
  3508.       RTL_EXPR_SEQUENCE (t) = 0;
  3509.       return t;
  3510.     }
  3511. }
  3512.  
  3513. /* Return an rtx representing the value of X * MULT + ADD.
  3514.    TARGET is a suggestion for where to store the result (an rtx).
  3515.    MODE is the machine mode for the computation.
  3516.    X and MULT must have mode MODE.  ADD may have a different mode.
  3517.    So can X (defaults to same as MODE).
  3518.    UNSIGNEDP is non-zero to do unsigned multiplication.
  3519.    This may emit insns.  */
  3520.  
  3521. rtx
  3522. expand_mult_add (x, target, mult, add, mode, unsignedp)
  3523.      rtx x, target, mult, add;
  3524.      enum machine_mode mode;
  3525.      int unsignedp;
  3526. {
  3527.   tree type = type_for_mode (mode, unsignedp);
  3528.   tree add_type = (GET_MODE (add) == VOIDmode
  3529.            ? type : type_for_mode (GET_MODE (add), unsignedp));
  3530.   tree result =  fold (build (PLUS_EXPR, type,
  3531.                   fold (build (MULT_EXPR, type,
  3532.                        make_tree (type, x),
  3533.                        make_tree (type, mult))),
  3534.                   make_tree (add_type, add)));
  3535.  
  3536.   return expand_expr (result, target, VOIDmode, 0);
  3537. }
  3538.  
  3539. /* Compute the logical-and of OP0 and OP1, storing it in TARGET
  3540.    and returning TARGET.
  3541.  
  3542.    If TARGET is 0, a pseudo-register or constant is returned.  */
  3543.  
  3544. rtx
  3545. expand_and (op0, op1, target)
  3546.      rtx op0, op1, target;
  3547. {
  3548.   enum machine_mode mode = VOIDmode;
  3549.   rtx tem;
  3550.  
  3551.   if (GET_MODE (op0) != VOIDmode)
  3552.     mode = GET_MODE (op0);
  3553.   else if (GET_MODE (op1) != VOIDmode)
  3554.     mode = GET_MODE (op1);
  3555.  
  3556.   if (mode != VOIDmode)
  3557.     tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
  3558.   else if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == CONST_INT)
  3559.     tem = GEN_INT (INTVAL (op0) & INTVAL (op1));
  3560.   else
  3561.     abort ();
  3562.  
  3563.   if (target == 0)
  3564.     target = tem;
  3565.   else if (tem != target)
  3566.     emit_move_insn (target, tem);
  3567.   return target;
  3568. }
  3569.  
  3570. /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
  3571.    and storing in TARGET.  Normally return TARGET.
  3572.    Return 0 if that cannot be done.
  3573.  
  3574.    MODE is the mode to use for OP0 and OP1 should they be CONST_INTs.  If
  3575.    it is VOIDmode, they cannot both be CONST_INT.  
  3576.  
  3577.    UNSIGNEDP is for the case where we have to widen the operands
  3578.    to perform the operation.  It says to use zero-extension.
  3579.  
  3580.    NORMALIZEP is 1 if we should convert the result to be either zero
  3581.    or one one.  Normalize is -1 if we should convert the result to be
  3582.    either zero or -1.  If NORMALIZEP is zero, the result will be left
  3583.    "raw" out of the scc insn.  */
  3584.  
  3585. rtx
  3586. emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep)
  3587.      rtx target;
  3588.      enum rtx_code code;
  3589.      rtx op0, op1;
  3590.      enum machine_mode mode;
  3591.      int unsignedp;
  3592.      int normalizep;
  3593. {
  3594.   rtx subtarget;
  3595.   enum insn_code icode;
  3596.   enum machine_mode compare_mode;
  3597.   enum machine_mode target_mode = GET_MODE (target);
  3598.   rtx tem;
  3599.   rtx last = 0;
  3600.   rtx pattern, comparison;
  3601.  
  3602.   if (mode == VOIDmode)
  3603.     mode = GET_MODE (op0);
  3604.  
  3605.   /* If one operand is constant, make it the second one.  Only do this
  3606.      if the other operand is not constant as well.  */
  3607.  
  3608.   if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
  3609.       || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
  3610.     {
  3611.       tem = op0;
  3612.       op0 = op1;
  3613.       op1 = tem;
  3614.       code = swap_condition (code);
  3615.     }
  3616.  
  3617.   /* For some comparisons with 1 and -1, we can convert this to 
  3618.      comparisons with zero.  This will often produce more opportunities for
  3619.      store-flag insns. */
  3620.  
  3621.   switch (code)
  3622.     {
  3623.     case LT:
  3624.       if (op1 == const1_rtx)
  3625.     op1 = const0_rtx, code = LE;
  3626.       break;
  3627.     case LE:
  3628.       if (op1 == constm1_rtx)
  3629.     op1 = const0_rtx, code = LT;
  3630.       break;
  3631.     case GE:
  3632.       if (op1 == const1_rtx)
  3633.     op1 = const0_rtx, code = GT;
  3634.       break;
  3635.     case GT:
  3636.       if (op1 == constm1_rtx)
  3637.     op1 = const0_rtx, code = GE;
  3638.       break;
  3639.     case GEU:
  3640.       if (op1 == const1_rtx)
  3641.     op1 = const0_rtx, code = NE;
  3642.       break;
  3643.     case LTU:
  3644.       if (op1 == const1_rtx)
  3645.     op1 = const0_rtx, code = EQ;
  3646.       break;
  3647.     }
  3648.  
  3649.   /* From now on, we won't change CODE, so set ICODE now.  */
  3650.   icode = setcc_gen_code[(int) code];
  3651.  
  3652.   /* If this is A < 0 or A >= 0, we can do this by taking the ones
  3653.      complement of A (for GE) and shifting the sign bit to the low bit.  */
  3654.   if (op1 == const0_rtx && (code == LT || code == GE)
  3655.       && GET_MODE_CLASS (mode) == MODE_INT
  3656.       && (normalizep || STORE_FLAG_VALUE == 1
  3657.       || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  3658.           && (STORE_FLAG_VALUE 
  3659.           == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
  3660.     {
  3661.       subtarget = target;
  3662.  
  3663.       /* If the result is to be wider than OP0, it is best to convert it
  3664.      first.  If it is to be narrower, it is *incorrect* to convert it
  3665.      first.  */
  3666.       if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
  3667.     {
  3668.       op0 = protect_from_queue (op0, 0);
  3669.       op0 = convert_modes (target_mode, mode, op0, 0);
  3670.       mode = target_mode;
  3671.     }
  3672.  
  3673.       if (target_mode != mode)
  3674.     subtarget = 0;
  3675.  
  3676.       if (code == GE)
  3677.     op0 = expand_unop (mode, one_cmpl_optab, op0, subtarget, 0);
  3678.  
  3679.       if (normalizep || STORE_FLAG_VALUE == 1)
  3680.     /* If we are supposed to produce a 0/1 value, we want to do
  3681.        a logical shift from the sign bit to the low-order bit; for
  3682.        a -1/0 value, we do an arithmetic shift.  */
  3683.     op0 = expand_shift (RSHIFT_EXPR, mode, op0,
  3684.                 size_int (GET_MODE_BITSIZE (mode) - 1),
  3685.                 subtarget, normalizep != -1);
  3686.  
  3687.       if (mode != target_mode)
  3688.     op0 = convert_modes (target_mode, mode, op0, 0);
  3689.  
  3690.       return op0;
  3691.     }
  3692.  
  3693.   if (icode != CODE_FOR_nothing)
  3694.     {
  3695.       /* We think we may be able to do this with a scc insn.  Emit the
  3696.      comparison and then the scc insn.
  3697.  
  3698.      compare_from_rtx may call emit_queue, which would be deleted below
  3699.      if the scc insn fails.  So call it ourselves before setting LAST.  */
  3700.  
  3701.       emit_queue ();
  3702.       last = get_last_insn ();
  3703.  
  3704.       comparison
  3705.     = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX, 0);
  3706.       if (GET_CODE (comparison) == CONST_INT)
  3707.     return (comparison == const0_rtx ? const0_rtx
  3708.         : normalizep == 1 ? const1_rtx
  3709.         : normalizep == -1 ? constm1_rtx
  3710.         : const_true_rtx);
  3711.  
  3712.       /* If the code of COMPARISON doesn't match CODE, something is
  3713.      wrong; we can no longer be sure that we have the operation.  
  3714.      We could handle this case, but it should not happen.  */
  3715.  
  3716.       if (GET_CODE (comparison) != code)
  3717.     abort ();
  3718.  
  3719.       /* Get a reference to the target in the proper mode for this insn.  */
  3720.       compare_mode = insn_operand_mode[(int) icode][0];
  3721.       subtarget = target;
  3722.       if (preserve_subexpressions_p ()
  3723.       || ! (*insn_operand_predicate[(int) icode][0]) (subtarget, compare_mode))
  3724.     subtarget = gen_reg_rtx (compare_mode);
  3725.  
  3726.       pattern = GEN_FCN (icode) (subtarget);
  3727.       if (pattern)
  3728.     {
  3729.       emit_insn (pattern);
  3730.  
  3731.       /* If we are converting to a wider mode, first convert to
  3732.          TARGET_MODE, then normalize.  This produces better combining
  3733.          opportunities on machines that have a SIGN_EXTRACT when we are
  3734.          testing a single bit.  This mostly benefits the 68k.
  3735.  
  3736.          If STORE_FLAG_VALUE does not have the sign bit set when
  3737.          interpreted in COMPARE_MODE, we can do this conversion as
  3738.          unsigned, which is usually more efficient.  */
  3739.       if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
  3740.         {
  3741.           convert_move (target, subtarget,
  3742.                 (GET_MODE_BITSIZE (compare_mode)
  3743.                  <= HOST_BITS_PER_WIDE_INT)
  3744.                 && 0 == (STORE_FLAG_VALUE
  3745.                      & ((HOST_WIDE_INT) 1
  3746.                     << (GET_MODE_BITSIZE (compare_mode) -1))));
  3747.           op0 = target;
  3748.           compare_mode = target_mode;
  3749.         }
  3750.       else
  3751.         op0 = subtarget;
  3752.  
  3753.       /* If we want to keep subexpressions around, don't reuse our
  3754.          last target.  */
  3755.  
  3756.       if (preserve_subexpressions_p ())
  3757.         subtarget = 0;
  3758.  
  3759.       /* Now normalize to the proper value in COMPARE_MODE.  Sometimes
  3760.          we don't have to do anything.  */
  3761.       if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
  3762.         ;
  3763.       else if (normalizep == - STORE_FLAG_VALUE)
  3764.         op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
  3765.  
  3766.       /* We don't want to use STORE_FLAG_VALUE < 0 below since this
  3767.          makes it hard to use a value of just the sign bit due to
  3768.          ANSI integer constant typing rules.  */
  3769.       else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
  3770.            && (STORE_FLAG_VALUE
  3771.                & ((HOST_WIDE_INT) 1
  3772.               << (GET_MODE_BITSIZE (compare_mode) - 1))))
  3773.         op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
  3774.                 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
  3775.                 subtarget, normalizep == 1);
  3776.       else if (STORE_FLAG_VALUE & 1)
  3777.         {
  3778.           op0 = expand_and (op0, const1_rtx, subtarget);
  3779.           if (normalizep == -1)
  3780.         op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
  3781.         }
  3782.       else
  3783.         abort ();
  3784.  
  3785.       /* If we were converting to a smaller mode, do the 
  3786.          conversion now.  */
  3787.       if (target_mode != compare_mode)
  3788.         {
  3789.           convert_move (target, op0, 0);
  3790.           return target;
  3791.         }
  3792.       else
  3793.         return op0;
  3794.     }
  3795.     }
  3796.  
  3797.   if (last)
  3798.     delete_insns_since (last);
  3799.  
  3800.   subtarget = target_mode == mode ? target : 0;
  3801.  
  3802.   /* If we reached here, we can't do this with a scc insn.  However, there
  3803.      are some comparisons that can be done directly.  For example, if
  3804.      this is an equality comparison of integers, we can try to exclusive-or
  3805.      (or subtract) the two operands and use a recursive call to try the
  3806.      comparison with zero.  Don't do any of these cases if branches are
  3807.      very cheap.  */
  3808.  
  3809.   if (BRANCH_COST > 0
  3810.       && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
  3811.       && op1 != const0_rtx)
  3812.     {
  3813.       tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
  3814.               OPTAB_WIDEN);
  3815.  
  3816.       if (tem == 0)
  3817.     tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
  3818.                 OPTAB_WIDEN);
  3819.       if (tem != 0)
  3820.     tem = emit_store_flag (target, code, tem, const0_rtx,
  3821.                    mode, unsignedp, normalizep);
  3822.       if (tem == 0)
  3823.     delete_insns_since (last);
  3824.       return tem;
  3825.     }
  3826.  
  3827.   /* Some other cases we can do are EQ, NE, LE, and GT comparisons with 
  3828.      the constant zero.  Reject all other comparisons at this point.  Only
  3829.      do LE and GT if branches are expensive since they are expensive on
  3830.      2-operand machines.  */
  3831.  
  3832.   if (BRANCH_COST == 0
  3833.       || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
  3834.       || (code != EQ && code != NE
  3835.       && (BRANCH_COST <= 1 || (code != LE && code != GT))))
  3836.     return 0;
  3837.  
  3838.   /* See what we need to return.  We can only return a 1, -1, or the
  3839.      sign bit.  */
  3840.  
  3841.   if (normalizep == 0)
  3842.     {
  3843.       if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  3844.     normalizep = STORE_FLAG_VALUE;
  3845.  
  3846.       else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
  3847.            && (STORE_FLAG_VALUE
  3848.            == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
  3849.     ;
  3850.       else
  3851.     return 0;
  3852.     }
  3853.  
  3854.   /* Try to put the result of the comparison in the sign bit.  Assume we can't
  3855.      do the necessary operation below.  */
  3856.  
  3857.   tem = 0;
  3858.  
  3859.   /* To see if A <= 0, compute (A | (A - 1)).  A <= 0 iff that result has
  3860.      the sign bit set.  */
  3861.  
  3862.   if (code == LE)
  3863.     {
  3864.       /* This is destructive, so SUBTARGET can't be OP0.  */
  3865.       if (rtx_equal_p (subtarget, op0))
  3866.     subtarget = 0;
  3867.  
  3868.       tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
  3869.               OPTAB_WIDEN);
  3870.       if (tem)
  3871.     tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
  3872.                 OPTAB_WIDEN);
  3873.     }
  3874.  
  3875.   /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
  3876.      number of bits in the mode of OP0, minus one.  */
  3877.  
  3878.   if (code == GT)
  3879.     {
  3880.       if (rtx_equal_p (subtarget, op0))
  3881.     subtarget = 0;
  3882.  
  3883.       tem = expand_shift (RSHIFT_EXPR, mode, op0,
  3884.               size_int (GET_MODE_BITSIZE (mode) - 1),
  3885.               subtarget, 0);
  3886.       tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
  3887.               OPTAB_WIDEN);
  3888.     }
  3889.                     
  3890.   if (code == EQ || code == NE)
  3891.     {
  3892.       /* For EQ or NE, one way to do the comparison is to apply an operation
  3893.      that converts the operand into a positive number if it is non-zero
  3894.      or zero if it was originally zero.  Then, for EQ, we subtract 1 and
  3895.      for NE we negate.  This puts the result in the sign bit.  Then we
  3896.      normalize with a shift, if needed. 
  3897.  
  3898.      Two operations that can do the above actions are ABS and FFS, so try
  3899.      them.  If that doesn't work, and MODE is smaller than a full word,
  3900.      we can use zero-extension to the wider mode (an unsigned conversion)
  3901.      as the operation.  */
  3902.  
  3903.       if (abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
  3904.     tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
  3905.       else if (ffs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
  3906.     tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
  3907.       else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
  3908.     {
  3909.       op0 = protect_from_queue (op0, 0);
  3910.       tem = convert_modes (word_mode, mode, op0, 1);
  3911.       mode = word_mode;
  3912.     }
  3913.  
  3914.       if (tem != 0)
  3915.     {
  3916.       if (code == EQ)
  3917.         tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
  3918.                 0, OPTAB_WIDEN);
  3919.       else
  3920.         tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
  3921.     }
  3922.  
  3923.       /* If we couldn't do it that way, for NE we can "or" the two's complement
  3924.      of the value with itself.  For EQ, we take the one's complement of
  3925.      that "or", which is an extra insn, so we only handle EQ if branches
  3926.      are expensive.  */
  3927.  
  3928.       if (tem == 0 && (code == NE || BRANCH_COST > 1))
  3929.     {
  3930.       if (rtx_equal_p (subtarget, op0))
  3931.         subtarget = 0;
  3932.  
  3933.       tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
  3934.       tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
  3935.                   OPTAB_WIDEN);
  3936.  
  3937.       if (tem && code == EQ)
  3938.         tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
  3939.     }
  3940.     }
  3941.  
  3942.   if (tem && normalizep)
  3943.     tem = expand_shift (RSHIFT_EXPR, mode, tem,
  3944.             size_int (GET_MODE_BITSIZE (mode) - 1),
  3945.             tem, normalizep == 1);
  3946.  
  3947.   if (tem && GET_MODE (tem) != target_mode)
  3948.     {
  3949.       convert_move (target, tem, 0);
  3950.       tem = target;
  3951.     }
  3952.  
  3953.   if (tem == 0)
  3954.     delete_insns_since (last);
  3955.  
  3956.   return tem;
  3957. }
  3958.